-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path224_Basic_Calculator.txt
More file actions
76 lines (66 loc) · 1.91 KB
/
224_Basic_Calculator.txt
File metadata and controls
76 lines (66 loc) · 1.91 KB
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
/* 224. Basic Calculator
Given a string s representing an expression, implement a basic calculator to evaluate it.
Example 1:
Input: s = "1 + 1"
Output: 2
Example 2:
Input: s = " 2-1 + 2 "
Output: 3
Example 3:
Input: s = "(1+(4+5+2)-3)+(6+8)"
Output: 23
*/
class Solution {
public int calculate(String s) {
HashMap<Integer, Integer> mapping = new HashMap<Integer, Integer>();
int i = 0;
ArrayList<Integer> stack = new ArrayList<Integer>();
while(i < s.length()) {
if(s.charAt(i) == '(') {
stack.add(i);
}
else if(s.charAt(i) == ')') {
mapping.put(stack.get(stack.size() - 1), i);
stack.remove(stack.size() - 1);
}
i++;
}
return calculate(s, 0, s.length() - 1, mapping);
}
private int calculate(String s, int start, int end, HashMap<Integer, Integer> mapping) {
if(start > end)
return 0;
int result = 0;
int curr = 0;
int currSign = 1;
int i = start;
while(i <= end) {
char currChar = s.charAt(i);
if(currChar == ' ') {
i++;
continue;
}
if(currChar == '(') {
curr = calculate(s, i + 1, mapping.get(i) - 1, mapping);
i = mapping.get(i) + 1;
continue;
}
if(currChar != '-' && currChar != '+') {
curr = curr * 10 + (int)currChar - 48;
}
else {
if(curr != 0) {
result = result + currSign * curr;
curr = 0;
}
if(currChar == '-')
currSign = -1;
else
currSign = 1;
}
i++;
}
result = result + currSign * curr;
return result;
}
}