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

Some examples:

"1 + 1" = 2
" 2-1 + 2 " = 3
"(1+(4+5+2)-3)+(6+8)" = 23

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

https://leetcode.com/problems/basic-calculator/



这这这不是我每天都为之纠结的后缀表达式吗。

题目比较简单只有加减法,而且没有算上负数。

我赌5毛,将来会有一道hard就是这题带上负数,小数,大小括号,乘除法之类,名字我都起好了Basic Calculator II。

2个栈,第一个是放运算结果(或者中间运算结果)的栈resultStack,第二个是放运算符的栈opStack。

举栗子:1+2

1 --> resultStack : [1], opStack : []

+ --> resultStack : [1], opStack : [+]

2 --> resultStack : [1,2], opStack : [+]

压缩二元运算操作compress_2operators() --> resultStack : [3], opStack : []

结束

举栗子2: 3-(2-1)

3 --> resultStack : [3], opStack : []

- --> resultStack : [3], opStack : [-]

( --> resultStack : [3], opStack : [-,(]

2 --> resultStack : [3,2], opStack : [-,(]

- --> resultStack : [3,2], opStack : [-,(,-]

1 --> resultStack : [3,2,1], opStack : [-,(,-]

) --> 压缩括号操作compress_bracket(),中间会调用compress_2operators() --> resultStack : [3,1], opStack : [-]

压缩二元运算操作compress_2operators() --> resultStack : [2], opStack : []

结束

如果要加上乘除法,需要修改一下正则,加上乘除法和加减法的优先级。

 1 /**
 2  * @param {string} s
 3  * @return {number}
 4  */
 5 var calculate = function(s) {
 6     var resultStack = [];
 7     var opStack = [];
 8     var temp = "";
 9     for(var i = 0; i < s.length; i++){
10         var ch = s[i];
11         if(/^(\+|\-)$/.test(ch)){ // + -
12             compress_2operators();
13             opStack.push(ch);
14         } else if (ch === ‘(‘){
15             opStack.push(ch);
16         } else if (ch === ‘)‘){
17             compress_bracket();
18         } else if (/^[0-9]$/.test(ch)){
19             temp += ch;
20         }
21
22         if(s[i + 1] && /^(\+|\-|\(|\))$/.test(s[i + 1])){ // + - ( )
23             if(temp !== ""){
24                 resultStack.push(parseInt(temp));
25                 temp = "";
26             }
27         }
28     }
29     if(temp !== ""){
30         resultStack.push(parseInt(temp));
31         temp = "";
32     }
33     compress_2operators();
34     return resultStack.pop();
35
36     function compress_bracket(){
37         while(opStack[opStack.length - 1] !== ‘(‘){
38             compress_2operators();
39         }
40         opStack.pop(); //(
41     }
42     function compress_2operators(){
43         while(/^(\+|\-)$/.test(opStack[opStack.length - 1])){ // + -
44             var op = opStack.pop();
45             var right = resultStack.pop();
46             var left = resultStack.pop();
47             if(op === ‘+‘){
48                 resultStack.push(left + right);
49             }else{
50                 resultStack.push(left - right);
51             }
52         }
53     }
54 };
时间: 2024-08-03 15:39:17

[LeetCode][JavaScript]Basic Calculator的相关文章

【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】Basic Calculator

题目链接:https://leetcode.com/problems/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 em

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

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 224: Basic Calculator

Basic Calculator Total Accepted: 2801 Total Submissions: 18103 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

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

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