ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode 772. Basic Calculator III

2019-12-08 09:05:17  阅读:294  来源: 互联网

标签:num2 get int expression Calculator Basic III o2 o1


原题链接在这里: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.

标签:num2,get,int,expression,Calculator,Basic,III,o2,o1
来源: https://www.cnblogs.com/Dylan-Java-NYC/p/12004567.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有