[LeetCode-JAVA] Basic Calculator II

题目:

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 should truncate toward zero.

You may assume that the given expression is always valid.

Some examples:

"3+2*2" = 7
" 3/2 " = 1
" 3+5 / 2 " = 5

Note: Do not use the eval built-in library function.

题意:简易计算器,可以进行四则运算

思路:在I的基础上可以很清晰的分析此题,利用两个Stack,一个存储数字,一个存储符号需要注意一下几点:

(1)连续的数字需要进行处理;

(2)乘除需要在后一个数字入栈后处理;

(3)最后栈内剩余为加减法,只需要一步步弹出即可,需要记住一次只弹出一个数字和符号,不能两个数字运算,在I中有说明,遗忘的话可以点击->  Basic Calculator I

(4)需要对最后一个数字进行特殊处理,因为前面的方法只有检测到有符号的时候才会将数字如栈。

代码(后面有优化版):

public class Solution {
    public int calculate(String s) {
        if(s == null || s.length() == 0)
            return 0;
        Stack<Integer> num = new Stack<Integer>();
        Stack<Character> symbol = new Stack<Character>();

        boolean isNum = false;
        int count = 0;
        for(int i = 0 ; i < s.length() ; i++){
            if(s.charAt(i) == ‘ ‘)
                continue;
            char temp = s.charAt(i);
            if(Character.isDigit(temp)){
                count = count * 10 + (temp - ‘0‘) ;
                isNum = true;
            }else if(isNum){
                num.push(count);
                count = 0;
                isNum = false;
                if(!symbol.isEmpty() && (symbol.peek() == ‘*‘ || symbol.peek() == ‘/‘)){
                    char tempSymbol = symbol.pop();
                    int b = num.pop();
                    int a = num.pop();
                    if(tempSymbol == ‘*‘){
                        num.push(a*b);
                    }else{
                        num.push(a/b);
                    }
                }
            }
            if(!Character.isDigit(temp)){
                symbol.push(temp);
            }
        }
        if(isNum){
            num.push(count);
            if(!symbol.isEmpty() && (symbol.peek() == ‘*‘ || symbol.peek() == ‘/‘)){
                char tempSymbol = symbol.pop();
                int b = num.pop();
                int a = num.pop();
                if(tempSymbol == ‘*‘){
                    num.push(a*b);
                }else{
                    num.push(a/b);
                }
            }
        }

        if(!symbol.isEmpty()){
            int rep = 0;
            while(!symbol.isEmpty()){
                char tempSymbol = symbol.pop();
                int a = num.pop();
                if(tempSymbol == ‘+‘){
                    rep+=a;
                }else{
                    rep-=a;
                }
            }
            num.push(rep + num.pop());
        }

        return num.pop();
    }
}

优化思路:注意到代码有冗余的部分,是因为每一次数字入栈是在判断有符号的时候,这时,可以人为的在初始出入的String的末尾加入非运算符号,这样可以将二者整合到一起。

优化后代码:

public class Solution {
    public int calculate(String s) {
        if(s == null || s.length() == 0)
            return 0;
        Stack<Integer> num = new Stack<Integer>();
        Stack<Character> symbol = new Stack<Character>();
        s += "#";
        boolean isNum = false;
        int count = 0;
        for(int i = 0 ; i < s.length() ; i++){
            if(s.charAt(i) == ‘ ‘)
                continue;
            char temp = s.charAt(i);
            if(Character.isDigit(temp)){
                count = count * 10 + (temp - ‘0‘) ; // 连续的数字
                isNum = true;
            }else{
                if(isNum){  // 遇到符号 将前面数字压入
                    num.push(count);
                    count = 0;
                    isNum = false;
                    if(!symbol.isEmpty() && (symbol.peek() == ‘*‘ || symbol.peek() == ‘/‘)){
                        char tempSymbol = symbol.pop();
                        int b = num.pop();
                        int a = num.pop();
                        if(tempSymbol == ‘*‘){
                            num.push(a*b);
                        }else{
                            num.push(a/b);
                        }
                    }
                }
                if(temp != ‘#‘){ // 将符号压入
                    symbol.push(temp);
                }
            }
        }

        if(!symbol.isEmpty()){
            int rep = 0;
            while(!symbol.isEmpty()){  // 加减运算
                char tempSymbol = symbol.pop();
                int a = num.pop();
                if(tempSymbol == ‘+‘){
                    rep+=a;
                }else{
                    rep-=a;
                }
            }
            num.push(rep + num.pop());
        }

        return num.pop();
    }
}
时间: 2024-10-14 03:56:10

[LeetCode-JAVA] Basic Calculator II的相关文章

LeetCode OJ Basic Calculator II

Basic Calculator II 题目 思路 和这个一样:Basic Calculator I 代码 class ExpressionTransformation { public: string trans_to_postfix_expression_to_s(string); // 将得到的表达式转化为后缀表达式 long long int calculate_from_postfix_expression(); // 根据后缀表达式计算值 private: vector<string

Leetcode 227. Basic Calculator II JAVA语言

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 should truncate toward zero. You may assume that the given ex

Java for LeetCode 227 Basic Calculator II

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 should truncate toward zero. You may assume that the given ex

leetcode 227. Basic Calculator II ---------- java

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 should truncate toward zero. You may assume that the given ex

【Leetcode】Basic Calculator II

题目链接:https://leetcode.com/problems/basic-calculator-ii/ 题目: 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 s

[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 should truncate toward zero. You may assume that the

LeetCode 227. 基本计算器 II(Basic Calculator II)

227. 基本计算器 II 227. Basic Calculator II 题目描述 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式仅包含非负整数,+,-,*,/ 四种运算符和空格.整数除法仅保留整数部分. LeetCode227. Basic Calculator II中等 示例?1: 输入: "3+2*2" 输出: 7 示例 2: 输入: " 3/2 " 输出: 1 示例 3: 输入: " 3+5 / 2 " 输出:

【LeetCode】227. Basic Calculator II

Basic Calculator II 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 should truncate toward zero. You may assu

[LeetCode][JavaScript]Basic Calculator

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 giv

Java for LeetCode 224 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