zoukankan      html  css  js  c++  java
  • Leetcode: Basic Calculator

    Implement a basic calculator to evaluate a simple expression string.
    
    The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
    
    You may assume that the given expression is always valid.
    
    Some examples:
    "1 + 1" = 2
    " 2-1 + 2 " = 3
    "(1+(4+5+2)-3)+(6+8)" = 23

    https://discuss.leetcode.com/topic/15816/iterative-java-solution-with-stack

    1. digit: it should be one digit from the current number
    2. '+': number is over, we can add the previous number and start a new number
    3. '-': same as above
    4. '(': push the previous result and the sign into the stack, set result to 0, just calculate the new result within the parenthesis.
    5. ')': pop out the top two numbers from stack, first one is the sign before this pair of parenthesis, second is the temporary result before this pair of parenthesis. We add them together.

    第二遍做法:遇到digit直接走到末尾,并且直接利用sign进行计算,这样比较方便

     1 public static int calculate(String s) {
     2     int len = s.length(), sign = 1, result = 0;
     3     Stack<Integer> stack = new Stack<Integer>();
     4     for (int i = 0; i < len; i++) {
     5         if (Character.isDigit(s.charAt(i))) {
     6             int sum = s.charAt(i) - '0';
     7             while (i + 1 < len && Character.isDigit(s.charAt(i + 1))) {
     8                 sum = sum * 10 + s.charAt(i + 1) - '0';
     9                 i++;
    10             }
    11             result += sum * sign;
    12         } else if (s.charAt(i) == '+')
    13             sign = 1;
    14         else if (s.charAt(i) == '-')
    15             sign = -1;
    16         else if (s.charAt(i) == '(') {
    17             stack.push(result);
    18             stack.push(sign);
    19             result = 0;
    20             sign = 1;
    21         } else if (s.charAt(i) == ')') {
    22             result = result * stack.pop() + stack.pop(); // notice 这里不是+=
    23         }
    24 
    25     }
    26     return result;
    27 }

    第一遍做法:

     1 public class Solution {
     2     public int calculate(String s) {
     3         if (s==null || s.length()==0) return 0;
     4         Stack<Integer> st = new Stack<Integer>();
     5         int sum = 0;
     6         int num = 0;
     7         int sign = 1;
     8         for (int i=0; i<s.length(); i++) {
     9             char cur = s.charAt(i);
    10             if (Character.isDigit(cur)) {
    11                 num = num*10 + (int)(cur-'0');
    12             }
    13             else if (cur == '+') {
    14                 sum += sign*num;
    15                 num = 0;
    16                 sign = 1;
    17             }
    18             else if (cur == '-') {
    19                 sum += sign*num;
    20                 num = 0;
    21                 sign = -1;
    22             }
    23             else if (cur == '(') {
    24                 st.push(sum);
    25                 st.push(sign);
    26                 sum = 0;
    27                 sign = 1;
    28             }
    29             else if (cur == ')') {
    30                 sum += sign*num;
    31                 num = 0;
    32                 sum *= st.pop();
    33                 sum += st.pop();
    34             }
    35 
    36         }
    37         if (num != 0) sum += sign*num;
    38         return sum;
    39     }
    40 }
  • 相关阅读:
    手工给alv添加合计
    双重loop循环时第二个loop用where…
    屏幕的双击事件(如在table&nbsp;c…
    根据CR获取CR下的开发对象
    不在se11建结构smartforms传值方法
    关于SMARTFORM的页数问题
    获取最大权限
    关于oracle的varchar2的比较大小的规律
    oracle中查询、禁用、启用、删除表外键
    关于oracle with as用法(比较简单的介绍)
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/5058595.html
Copyright © 2011-2022 走看看