码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 772. Basic Calculator III

时间:2019-12-08 11:06:22      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:UNC   ring   media   first   col   push   sum   stack   involve   

原题链接在这里: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         int n = s.length();
 8         int num1 = 0;
 9         int o1 = 1;
10         int num2 = 1;
11         int o2 = 1;
12         Stack<Integer> stk = new Stack<>();
13         
14         for(int i = 0; i<n; i++){
15             char c = s.charAt(i);
16             if(Character.isDigit(c)){
17                 int cur = 0;
18                 while(i < n && Character.isDigit(s.charAt(i))){
19                     cur = cur * 10 + (s.charAt(i) - ‘0‘);
20                     i++;
21                 }
22                 
23                 i--;
24                 num2 = o2 == 1 ? num2 * cur : num2 / cur;
25             }else if(c == ‘*‘ || c == ‘/‘){
26                 o2 = c == ‘*‘ ? 1 : -1;
27             }else if(c == ‘(‘){
28                 stk.push(num1);
29                 stk.push(o1);
30                 stk.push(num2);
31                 stk.push(o2);
32                 
33                 num1 = 0;
34                 o1 = 1;
35                 num2 = 1;
36                 o2 = 1;
37             }else if(c == ‘+‘ || c == ‘-‘){
38                 if(c == ‘-‘ && (i == 0 || s.charAt(i-1) == ‘(‘)){
39                     o1 = -1;
40                     continue;
41                 }
42                 
43                 num1 = num1 + o1 * num2;
44                 o1 = (c == ‘+‘ ? 1 : -1);
45                 
46                 num2 = 1;
47                 o2 = 1;
48             }else if(c == ‘)‘){
49                 int cur = num1 + o1 * num2;
50                 o2 = stk.pop();
51                 num2 = stk.pop();
52                 o1 = stk.pop();
53                 num1 = stk.pop();
54                 
55                 num2 = o2 == 1 ? num2 * cur : num2 / cur;
56             }
57         }
58         
59         return num1 + o1 * num2;
60     }
61 }

类似Basic CalculatorBasic Calculator II.

LeetCode 772. Basic Calculator III

标签:UNC   ring   media   first   col   push   sum   stack   involve   

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12004567.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!