-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode224.java
More file actions
124 lines (119 loc) · 4.49 KB
/
Leetcode224.java
File metadata and controls
124 lines (119 loc) · 4.49 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package codeTop;
import java.util.Stack;
public class Leetcode224 {
public static int calculate(String s) {
if(s == null || s.length() == 0){
return 0;
}
int res = 0,idx = 0;
Stack<Integer> stack = new Stack<>();
s = s.trim();
int n = s.length();
while(idx < n){
char c = s.charAt(idx++);
if(c == ' '){
continue;
}
if(Character.isDigit(c)){
int tmp = c - '0';
while(idx < n && Character.isDigit(s.charAt(idx))){
tmp = tmp * 10 + s.charAt(idx++) - '0';
}
stack.push(tmp);
}else if(c == '+'){
if(Character.isDigit(s.charAt(idx))){
int tmp = s.charAt(idx++) - '0';
while(idx < s.length() && Character.isDigit(s.charAt(idx))){
tmp = tmp * 10 + s.charAt(idx++) - '0';
}
stack.push(tmp);
}else {
//碰到了(
int[] ans = process(s,++idx);
stack.push(ans[0]);
idx = ans[1];
}
}else if(c == '-'){
if(Character.isDigit(s.charAt(idx))){
int tmp = s.charAt(idx++) - '0';
while(idx < s.length() && Character.isDigit(s.charAt(idx))){
tmp = tmp * 10 + s.charAt(idx++) - '0';
}
stack.push(-tmp);
}else {
//碰到了(
int[] ans = process(s,++idx);
stack.push(-ans[0]);
idx = ans[1];
}
}else{
int[] ans = process(s,idx);
stack.push(ans[0]);
idx = ans[1];
}
}
while(!stack.isEmpty()){
res += stack.pop();
}
return res;
}
/**
*碰到了(的处理
*/
private static int[] process(String s,int idx){
int[] ans = new int[2];
Stack<Integer> stack = new Stack<>();
while(idx < s.length() && s.charAt(idx) != ')'){
char c = s.charAt(idx++);
if(c == ' '){
continue;
}
if(Character.isDigit(c)){
int tmp = c - '0';
while(idx < s.length() && Character.isDigit(s.charAt(idx))){
tmp = tmp * 10 + s.charAt(idx++) - '0';
}
stack.push(tmp);
}else if(c == '+'){
if(Character.isDigit(s.charAt(idx))){
int tmp = s.charAt(idx++) - '0';
while(idx < s.length() && Character.isDigit(s.charAt(idx))){
tmp = tmp * 10 + s.charAt(idx++) - '0';
}
stack.push(tmp);
}else {
//碰到了(
int[] help = process(s,++idx);
stack.push(help[0]);
idx = help[1];
}
}else if(c == '-'){
if(Character.isDigit(s.charAt(idx))){
int tmp = s.charAt(idx++) - '0';
while(idx < s.length() && Character.isDigit(s.charAt(idx))){
tmp = tmp * 10 + s.charAt(idx++) - '0';
}
stack.push(-tmp);
}else {
//碰到了(
int[] help = process(s,++idx);
stack.push(-help[0]);
idx = help[1];
}
}else if(c == '('){
int[] help = process(s,idx);
stack.push(help[0]);
idx = help[1];
}
}
while(!stack.isEmpty()){
ans[0] += stack.pop();
}
ans[1] = ++idx;
return ans;
}
public static void main(String[] args) {
String s = "(1+(4+5+2)-3)+(6+8)";
System.out.println(calculate(s));
}
}