• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
neverlandly
博客园    首页    新随笔    联系   管理    订阅  订阅

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 }

 

posted @ 2015-12-19 06:12  neverlandly  阅读(371)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3