forked from szl0072/Leetcode-Solution-Code
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBasicCalculatorII.java
88 lines (83 loc) · 2.41 KB
/
BasicCalculatorII.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package leetcode;
import java.util.Stack;
/**
* Project Name : Leetcode
* Package Name : leetcode
* File Name : BasicCalculatorII
* Creator : Edward
* Date : Sep, 2017
* Description : 227. Basic Calculator II
*/
public class BasicCalculatorII {
/**
*
"3+2*2" = 7
" 3/2 " = 1
" 3+5 / 2 " = 5
3*2+4-5/2
* @param s
* @return
*/
//time : O(n) space : O(n)
public int calculate(String s) {
if (s == null || s.length() == 0) return 0;
Stack<Integer> stack = new Stack<>();
int res = 0;
char sign = '+';
int num = 0;
for (int i = 0; i < s.length(); i++) {
if (Character.isDigit(s.charAt(i))) {
num = s.charAt(i) - '0';
while (i + 1 < s.length() && Character.isDigit(s.charAt(i + 1))) {
num = num * 10 + s.charAt(i + 1) - '0';
i++;
}
}
if (!Character.isDigit(s.charAt(i)) && s.charAt(i) != ' ' || i == s.length() - 1) {
if (sign == '+') stack.push(num);
if (sign == '-') stack.push(-num);
if (sign == '*') stack.push(stack.pop() * num);
if (sign == '/') stack.push(stack.pop() / num);
sign = s.charAt(i);
num = 0;
}
}
for (int i : stack) {
res += i;
}
return res;
}
// time : O(n) space : O(1)
public int calculate2(String s) {
if (s == null || s.length() == 0) return 0;
s = s.trim().replaceAll(" +", "");
int res = 0;
int preVal = 0;
int i = 0;
char sign = '+';
while (i < s.length()) {
int curVal = 0;
while (i < s.length() && Character.isDigit(s.charAt(i))) {
curVal = curVal * 10 + s.charAt(i) - '0';
i++;
}
if (sign == '+') {
res += preVal;
preVal = curVal;
} else if (sign == '-') {
res += preVal;
preVal = -curVal;
} else if (sign == '*') {
preVal = preVal * curVal;
} else if (sign == '/') {
preVal = preVal / curVal;
}
if (i < s.length()) {
sign = s.charAt(i);
i++;
}
}
res += preVal;
return res;
}
}