资讯专栏INFORMATION COLUMN

224. Basic Calculator & 227. Basic Calculator

_DangJin / 470人阅读

摘要:题目链接,就是感觉条件有点多简单点的写法,把直接用存在里面,就存成,存成题目链接这题就是碰到在加减后面怎么处理的问题。用一个来表示之前的,所以碰到现在是乘的时候就,除类似。

224. Basic Calculator

题目链接:https://leetcode.com/problems...

stack,就是感觉条件有点多

public class Solution {
    public int calculate(String s) {
        Stack nums = new Stack();
        Stack signs = new Stack();
        int prev = 0,  cur = 0;
        char sign = "#";
        int i = 0;
        /* prev: number before sign, cur: number after sign, sign
         * push: meet "(", put prev, sign = "#"
         * pop: meet ")" if not empty
         * number: do caculate if sign != "#"
         */
        // start of number or sign or "(" or ")"
        while(i < s.length()) {
            char c = s.charAt(i);
            // number
            if(Character.isDigit(c)) {
                while(i < s.length() && Character.isDigit(s.charAt(i))) cur = cur * 10 + (s.charAt(i++) - "0");
                i--;
                if(sign == "+")  prev += cur;
                else if(sign == "-")  prev -= cur;
                else prev = cur;
                cur = 0;
            }
            else if(c == "(") {
                // in case of "(1 + 2)"
                if(sign != "#") {
                    nums.push(prev);
                    signs.push(sign);
                    sign = "#";
                    prev = 0;
                }
            }
            else if(c == ")") {
                if(!nums.isEmpty()) {
                    int temp = nums.pop();
                    sign = signs.pop();
                    if(sign == "+") prev += temp;
                    else prev = temp - prev;
                    sign = "#";
                }
            }
            else if(c == "+" || c == "-") sign = c;
            // update
            i++;
        }
        
        return prev;
    }
}

简单点的写法,把sign直接用int存在stack里面,"+"就存成1, "-"存成-1

public class Solution {
    public int calculate(String s) {
        Stack nums = new Stack();
        Stack signs = new Stack();
        int prev = 0,  cur = 0;
        int sign = 1;
        int i = 0;
        /* prev: number before sign, cur: number after sign, sign
         * push: meet "(", put prev
         * pop: meet ")" if not empty
         * number: do caculate 
         */
        // start of number or sign or "(" or ")"
        while(i < s.length()) {
            char c = s.charAt(i);
            // number
            if(Character.isDigit(c)) {
                while(i < s.length() && Character.isDigit(s.charAt(i))) {
                    cur = cur * 10 + (s.charAt(i++) - "0");
                }
                i--;
                prev = prev + sign*cur;
                cur = 0;
            }
            else if(c == "(") {
                nums.push(prev);
                signs.push(sign);
                sign = 1;
                prev = 0;
            }
            else if(c == ")") {
                prev = nums.pop() + signs.pop() * prev;
            }
            else if(c == "+") sign = 1;
            else if(c == "-") sign = -1;
            // update
            i++;
        }
        
        return prev;
    }
}
227. Basic Calculator II

题目链接:https://leetcode.com/problems...

这题就是碰到"*", "/"在加减后面怎么处理的问题。用一个prev来表示之前的number,所以碰到现在是乘的时候就:res = res - prev + prev * cur, prev = prev * cur,除类似。

public class Solution {
    public int calculate(String s) {
        int res = 0;
        int prev = 0;
        s = s.replaceAll("[^0-9^+-/*]", "");
        int i = 0, n = s.length();
        // first number
        while(i < n && Character.isDigit(s.charAt(i))) {
            prev = prev * 10 + (s.charAt(i++) - "0");
        }
        res = prev;
        // loop invariant: start from sign: -, +, *, /
        while(i < n) {
            char c = s.charAt(i++);
            int cur = 0;
            while(i < n && Character.isDigit(s.charAt(i))) {
                cur = cur * 10 + (s.charAt(i++) - "0");
            }
            if(c == "+") {
                res += cur;
                prev = cur;
            }
            else if(c == "-") {
                res -= cur;
                prev = -cur;
            }
            else if(c == "*") {
                res = res - prev + prev * cur;
                prev *= cur;
            }
            else if(c == "/") {
                res = res - prev + prev / cur;
                prev /= cur;
            }
        }
        return res;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66680.html

相关文章

  • Leetcode[227] Basic Calculator II

    摘要:复杂度思路用两个来分别记录当前的结果和操作符注意每一次统计当前的的时候,要看一下下一位的操作符。有一种的方法,是表示的是匹配任意的空白符,包括空格,制表符,换行符,中文全角空格等。也可以用更简单的方法,。 LeetCode[227] Basic Calculator II Implement a basic calculator to evaluate a simple expres...

    chaos_G 评论0 收藏0
  • [LeetCode] 227. Basic Calculator II

    Problem Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division sho...

    silvertheo 评论0 收藏0
  • Leetcode[224] Basic Calculator

    摘要:复杂度思路将字符串先转换成后缀表达式,再将其出来。 Leetcode[224] Basic Calculator Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ),...

    William_Sang 评论0 收藏0
  • 227. Basic Calculator II

    摘要:但是乘除就会有问题,要特殊处理。这题只有加减和括号,优先级就是括号里的先计算,所有我们把括号里的内容当做操作的基本单位。遇到遇到和,遇到遇到,弹出再遇到弹出,这里只是把对数字的操作变成了对的操作,去括号的逻辑一样。 The expression string contains only non-negative integers, +, -, *, / operators and em...

    littlelightss 评论0 收藏0
  • [LeetCode] 224. Basic Calculator

    Problem 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 e...

    xiangzhihong 评论0 收藏0

发表评论

0条评论

_DangJin

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<