-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path224.java
More file actions
173 lines (154 loc) · 4.99 KB
/
224.java
File metadata and controls
173 lines (154 loc) · 4.99 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
__________________________________________________________________________________________________
sample 1 ms submission
class Solution {
public int calculate(String s) {
if( s == null || s.length() == 0 ) return 0;
return calculate21( s.toCharArray(), 0, new int[]{0});
}
private int calculate21( char[] s, int index, int[] next ){
int sum= 0, n=0, sign = 1;
for( int i = index; i < s.length; i ++ ){
char c = s[i];
if( c == '+' || c == '-'){
sum += n*sign;
sign = 1;
if( c == '-') sign = -1;
n=0;
}else if( c >= '0' && c <= '9'){
n = n*10 + (c-'0');
}else if( c == '('){
n = calculate21( s, i+1, next );
i = next[0];
}else if( c == ')'){
next[0] = i;
break;
}
}
sum += n*sign;
return sum;
}
private int calculate2( String s, int index, int[] next ){
int sum = 0, n = 0;
int sign = 1;
for( int i = index; i < s.length(); i ++ ){
char c = s.charAt(i);
if( c == '+' || c == '-'){
sum += n*sign;
sign = 1;
if( c == '-') sign = -1;
n = 0;
}else if( c >= '0' && c <= '9'){
n = n*10 + (c-'0');
}else if( c == '('){
n = calculate2( s, i+1, next );
sum += n*sign;
n = 0;
i = next[0];
}else if( c == ')'){
next[0] = i;
break;
}
}
sum += n*sign;
return sum;
}
private int calculate( String s, int index, int[] next ){
int sum = 0, n = 0;
boolean positive = true;
while( index < s.length() ){
char c = s.charAt(index);
if( c == '(' ){
n = calculate( s, index+1, next);
index = next[0];
continue;
}else if( c == ')'){
sum += positive ? n : -n;
next[0] = index + 1;
return sum;
}else if( c >= '0' && c <= '9'){
n = n*10 + (c-'0');
}else if( c == '+' || c == '-'){
sum += (positive?n : -n);
n = 0;
if( c == '-') positive = false;
else positive = true;
}
index ++;
}
sum += positive ? n : -n;
next[0] = index + 1;
return sum;
}
}
__________________________________________________________________________________________________
sample 35964 kb submission
class Solution {
public int calculate(String s) {
int m = (s == null) ? 0 : s.length();
if (m == 0) {
return 0;
}
Deque<Integer> stack = new ArrayDeque<>();
int current = 0;
int sign = 1;
int index = 0;
while (index < m) {
char c = s.charAt(index);
if (Character.isSpace(c)) {
++index;
continue;
} else if (Character.isDigit(c)) {
int left = index++;
while (index < m && Character.isDigit(s.charAt(index))) {
++index;
}
current += (sign) * Integer.parseInt(s.substring(left, index));
sign = 1;
continue;
} else if (c == '(') {
stack.push(current);
stack.push(sign);
current = 0;
sign = 1;
} else if (c == ')') {
int prevSign = stack.isEmpty() ? 1 : stack.pop();
int prevVal = stack.isEmpty() ? 0 : stack.pop();
current = prevVal + prevSign * current;
sign = 1;
} else {
sign = (c == '+') ? 1 : -1;
}
++index;
}
return current;
}
}
__________________________________________________________________________________________________
class Solution {
public static int calculate(String s) {
int len = s.length(), sign = 1, result = 0;
Stack<Integer> stack = new Stack<Integer>();
for (int i = 0; i < len; i++) {
if (Character.isDigit(s.charAt(i))) {
int sum = s.charAt(i) - '0';
while (i + 1 < len && Character.isDigit(s.charAt(i + 1))) {
sum = sum * 10 + s.charAt(i + 1) - '0';
i++;
}
result += sum * sign;
} else if (s.charAt(i) == '+')
sign = 1;
else if (s.charAt(i) == '-')
sign = -1;
else if (s.charAt(i) == '(') {
stack.push(result);
stack.push(sign);
result = 0;
sign = 1;
} else if (s.charAt(i) == ')') {
result = result * stack.pop() + stack.pop();
}
}
return result;
}
}