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 }

 

转载于:https://www.cnblogs.com/EdwardLiu/p/5058595.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值