224. Basic Calculator; 227. Basic Calculator II; 772. Basic Calculator III

问题:

给定算数字符串,求解。

其中字符串包含:

  • 数字:
    • '0'~'9':可构成任意n位数十进制数。
  • 运算符号:
    • '-':减法
    • '+':加法
    • '*':乘法(优先级高)
    • '/':除法(优先级高)
  • 括号:
    • '(':优先计算括号内
    • ')':优先计算括号内
  • 空格:
    • ' ':无意义
Example 1:
Input: s = "1 + 1"
Output: 2

Example 2:
Input: s = " 2-1 + 2 "
Output: 3

Example 3:
Input: s = "(1+(4+5+2)-3)+(6+8)"
Output: 23

Constraints:
1 <= s.length <= 3 * 10^5
s consists of digits, '+', '-', '(', ')', and ' '.
s represents a valid expression.

Example 1:
Input: s = "3+2*2"
Output: 7

Example 2:
Input: s = " 3/2 "
Output: 1

Example 3:
Input: s = " 3+5 / 2 "
Output: 5
 
Constraints:
1 <= s.length <= 3 * 10^5
s consists of integers and operators ('+', '-', '*', '/') separated by some number of spaces.
s represents a valid expression.
All the integers in the expression are non-negative integers in the range [0, 231 - 1].
The answer is guaranteed to fit in a 32-bit integer.

  

解法:stack+递归

思路:

将+-号和其后面的数值tmp,合并,存入stack中,

最后求stack所有元素和。

 

遍历各个字母:

遇到以下情况要做的处理:

  • 空格:不处理,continue
  • 数字:累计,记录当前数字tmp:tmp*10+当前位数字
  • 运算符号:
    • 合并上一个tmp+'+''-'号结果,存入stack
    • 若上一个运算符为'*''/'则进行运算,stack.top和tmp,将结果存入stack
  • 括号:递归调用处理函数helper,计算'('')'之间的结果,得到结果合并为tmp。

代码参考:

224. Basic Calculator
 1 class Solution {
 2 public:
 3     int calculate(string s) {
 4         int i=0;
 5         return helper(s, i);
 6     }
 7     int helper(string& s, int& idx) {
 8         stack<int> stk;
 9         int res=0;
10         int tmp=0;
11         bool isneg=false;
12         int i=idx;
13         for(i=idx; i<s.size() && s[i]!=')'; i++) {
14             if(s[i]=='(') tmp=helper(s, ++i);
15             else if (s[i]==' ') continue;
16             else if (s[i]>='0' && s[i]<='9') {
17                 tmp=((tmp*10)+(s[i]-'0'));
18             } else {
19                 stk.push(isneg?(-tmp):tmp);
20                 isneg = s[i]=='-';
21                 tmp = 0;
22             }
23         }
24         stk.push(isneg?(-tmp):tmp);
25         idx=i;
26         return sum(stk);
27     }
28     int sum(stack<int>& s) {
29         int res = 0;
30         while(!s.empty()) {
31             res+=s.top();
32             s.pop();
33         }
34         return res;
35     }
36 };

 

227. Basic Calculator II

 

 1 class Solution {
 2 public:
 3     int calculate(string s) {
 4         int i=0;
 5         return helper(s, i);
 6     }
 7     int helper(string s, int idx) {
 8         int res=0;
 9         stack<int> stk;
10         int tmp=0;
11         char sign='+';
12         int i=idx;
13         for(i=idx; i<=s.size(); i++) {
14             if(i!=s.size() && s[i]==' ') continue;
15             else if(i!=s.size() && s[i]>='0' && s[i]<='9') tmp=tmp*10+(s[i]-'0');
16             else {
17                 switch(sign) {
18                     case '+':
19                         stk.push(tmp);
20                         break;
21                     case '-':
22                         stk.push(-tmp);
23                         break;
24                     case '*':
25                         tmp*=stk.top();
26                         stk.pop();
27                         stk.push(tmp);
28                         break;
29                     case '/':
30                         tmp=stk.top()/tmp;
31                         stk.pop();
32                         stk.push(tmp);
33                         break;
34                 }
35                 tmp=0;
36                 sign=s[i];
37             }
38         }
39         return sum(stk);
40     }
41     int sum(stack<int>& stk) {
42         int res=0;
43         while(!stk.empty()) {
44             res+=stk.top();
45             stk.pop();
46         }
47         return res;
48     }
49 };

 

 

 

posted @ 2021-05-16 18:16  habibah_chang  阅读(46)  评论(0编辑  收藏  举报