LeetCode 772. Basic Calculator III

原题链接在这里:https://leetcode.com/problems/basic-calculator-iii/

题目:

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 .

The expression string contains only non-negative integers, +-*/ operators , open ( and closing parentheses ) and empty spaces . The integer division should truncate toward zero.

You may assume that the given expression is always valid. All intermediate results will be in the range of [-2147483648, 2147483647].

Some examples:

"1 + 1" = 2
" 6-4 / 2 " = 4
"2*(5+5*2)/3+(6/2+8)" = 21
"(2+6* 3+5- (3*14/7+2)*5)+3"=-12

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

题解:

Now the calculator involves + - * /  and ( ).

Here could separate + - * / into 2 groups. First calculate * /, then + -. 

+, o1 = 1. -, o1 = -1. 

*, o2 = 1. /, o2 = -1.

res = num1 + o1 * num2. num2 already has result with * /.

When encountering number, get the current number, and calculate on o2 and get num2.

When encountering (, push all the current result into stack and get reset values.

When encountering ), first get current number and then update num2 with * and /.

Note: corner case, - at index 0 or after (, need to update o1 and continue.

Time Complexity: O(n). n = s.length.

Space: O(n).

AC Java:

 1 class Solution {
 2     public int calculate(String s) {
 3         if(s == null || s.length() == 0){
 4             return 0;
 5         }
 6 
 7         s = s.replaceAll("\\s+", "");
 8         int n = s.length();
 9         int num1 = 0;
10         int o1 = 1;
11         int num2 = 1;
12         int o2 = 1;
13         Stack<Integer> stk = new Stack<>();
14 
15         for(int i = 0; i<n; i++){
16             char c = s.charAt(i);
17             if(Character.isDigit(c)){
18                 int cur = 0;
19                 while(i < n && Character.isDigit(s.charAt(i))){
20                     cur = cur * 10 + (s.charAt(i) - '0');
21                     i++;
22                 }
23 
24                 i--;
25                 num2 = o2 == 1 ? num2 * cur : num2 / cur;
26             }else if(c == '*' || c == '/'){
27                 o2 = c == '*' ? 1 : -1;
28             }else if(c == '('){
29                 stk.push(num1);
30                 stk.push(o1);
31                 stk.push(num2);
32                 stk.push(o2);
33 
34                 num1 = 0;
35                 o1 = 1;
36                 num2 = 1;
37                 o2 = 1;
38             }else if(c == '+' || c == '-'){
39                 if(c == '-' && (i == 0 || s.charAt(i-1) == '(')){
40                     o1 = -1;
41                     continue;
42                 }
43 
44                 num1 = num1 + o1 * num2;
45                 o1 = (c == '+' ? 1 : -1);
46 
47                 num2 = 1;
48                 o2 = 1;
49             }else if(c == ')'){
50                 int cur = num1 + o1 * num2;
51                 o2 = stk.pop();
52                 num2 = stk.pop();
53                 o1 = stk.pop();
54                 num1 = stk.pop();
55 
56                 num2 = o2 == 1 ? num2 * cur : num2 / cur;
57             }
58         }
59 
60         return num1 + o1 * num2;
61     }
62 }

类似Basic CalculatorBasic Calculator II.

posted @ 2019-12-08 08:35  Dylan_Java_NYC  阅读(457)  评论(0编辑  收藏  举报