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 empty spaces .

Example 1:

Input: "1 + 1"
Output: 2

Example 2:

Input: " 2-1 + 2 "
Output: 3

Example 3:

Input: "(1+(4+5+2)-3)+(6+8)"
Output: 23
Note:
  • You may assume that the given expression is always valid.
  • Do not use the eval built-in library function.

 

Solution:

  两种解法,递归解法碰到左括号时,找到与之匹配的右括号,将括号内的字符串调用递归函数进行计算,得到最后结果为止,递归解法不需要用栈,但是由于提取子字符串的操作导致效率低下。

 1 class Solution {
 2 public:
 3     int calculate(string s) {
 4         int pointer = 0;
 5         int number = 0;
 6         int sign = 1;
 7         int result = 0;
 8         while(pointer != s.size()){
 9             if(s[pointer] >= '0' && s[pointer] <= '9'){
10                 number = number*10+s[pointer]-'0';
11             }
12             else if(s[pointer] == '('){
13                 int left = pointer;
14                 int right = pointer;
15                 int count = 1;
16                 while(count != 0){
17                     right++;
18                     if(s[right] == '(')
19                         count++;
20                     else if(s[right] == ')')
21                         count--;
22                 }
23                 result += sign*calculate(s.substr(left+1,right-left-1));
24                 pointer = right;
25             }
26             else if(s[pointer] == '-' || s[pointer] == '+' || pointer == s.size()-1){
27                 result += sign*number;
28                 number = 0;
29                 sign = (s[pointer] == '+')?1:-1;
30             }
31             pointer++;
32         }
33         result += sign*number;
34         return result;
35     }
36 };

  第二种用栈的解法,当碰到左括号时,栈中压入之前得到的结果以及符号,当碰到右括号时,将括号内的结果乘以栈顶符号再加上符号左侧的结果,效率高。

Code:

 

 1 class Solution {
 2 public:
 3     int calculate(string s) {
 4         stack<int> stk;
 5         int pointer = 0;
 6         int number = 0;
 7         int sign = 1;
 8         int result = 0;
 9         while(pointer != s.size()){
10             if(s[pointer] >= '0' && s[pointer] <= '9'){
11                 number = number*10+s[pointer]-'0';
12             }
13             else if(s[pointer] == '('){
14                 stk.push(result);
15                 stk.push(sign);
16                 result = 0;
17                 sign = 1;
18             }
19             else if(s[pointer] == ')'){
20                 result += sign*number;
21                 number = 0;
22                 result *= stk.top();
23                 stk.pop();
24                 result += stk.top();
25                 stk.pop();
26             }
27             else if(s[pointer] == '-' || s[pointer] == '+' || pointer == s.size()-1){
28                 result += sign*number;
29                 number = 0;
30                 sign = (s[pointer] == '+')?1:-1;
31             }
32             pointer++;
33         }
34         result += sign*number;
35         return result;
36     }
37 };

 

posted on 2019-01-11 11:01  周浩炜  阅读(147)  评论(0编辑  收藏  举报

导航