[leedcode 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 expression is always valid.

Some examples:

"3+2*2" = 7
" 3/2 " = 1
" 3+5 / 2 " = 5
public class Solution {
    public int calculate(String s) {
        //使用sigh代表前一个运算符,利用栈保存加法或减法运算值,当遇到乘除时,
        //弹出栈顶进行运算,并将结果压栈,最后的结果时将栈中的元素进行相加
        int res=0;
        int sum=0;
        char sigh=‘+‘;
        Stack<Integer> stack=new Stack<Integer>();
        for(int i=0;i<s.length();i++){
            char c=s.charAt(i);
            if(Character.isDigit(c)){
                sum=sum*10+c-‘0‘;
            }
            if(!Character.isDigit(c)&&s.charAt(i)!=‘ ‘||i==s.length()-1){
                 if(sigh==‘+‘){
                    stack.push(sum);
                }else if(sigh==‘-‘){
                    stack.push(-sum);
                }else if(sigh==‘*‘){
                    int t=stack.pop();
                    stack.push(t*sum);
                }else if(sigh==‘/‘){
                    int t=stack.pop();
                    stack.push(t/sum);
                }
                sum=0;
                sigh=c;
            }
        }
        while(!stack.isEmpty()){
            res+=stack.pop();
        }
        return res;

    }
}
时间: 2024-11-19 17:20:14

[leedcode 227] Basic Calculator II的相关文章

【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 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#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

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

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 give

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

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. Example 1: Input: "3+2*2&q

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 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