-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathBasicCalculatorII.java
More file actions
151 lines (143 loc) · 4.49 KB
/
BasicCalculatorII.java
File metadata and controls
151 lines (143 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
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
package stack;
// Source : https://leetcode.com/problems/basic-calculator-ii/
// Id : 227,mst16.26
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2022/2/8
// Topic : stack
// Level : Medium
// Other :
// Tips :
// Links :
// Result : 100% 23%
import java.util.ArrayList;
import java.util.Stack;
public class BasicCalculatorII {
// 37 ms
// 适应性有点差
public int calculate(String s) {
Stack<Integer> operandStack = new Stack<>();
Stack<Character> operatorStack = new Stack<>();
int i = 0;
while (i < s.length()) {
char c = s.charAt(i);
if (Character.isDigit(c)) {
int val = c - '0';
while (Character.isDigit(s.charAt(i + 1))) {
i++;
val = val * 10 + (s.charAt(i) - '0');
}
if (operatorStack.isEmpty() || operatorStack.peek() == '+' || operatorStack.peek() == '-')
operandStack.push(val);
else {
char op = operatorStack.pop();
if (op == '*')
operandStack.push(val * operandStack.pop());
else
operandStack.push(operandStack.pop() / val);
}
} else if (c == ' ') {
} else {
operatorStack.push(c);
}
i++;
}
int res = 0;
while (!operatorStack.isEmpty()) {
char op = operatorStack.pop();
int tmp = operandStack.pop();
if (op == '+')
res += tmp;
else
res -= tmp;
}
return res + operandStack.pop();
}
// 4ms
// 用preSign preNum 起到栈的作用,用队列存储多出来的并且最后可以用累加计算的preNum,
public int calculate2(String s) {
int preNum = 0;
char preSign = '+';
int num = 0;
ArrayList<Integer> arr = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (Character.isDigit(c)) {
num = 0;
for (; i < s.length() && Character.isDigit(s.charAt(i)); i++) {
num = num * 10 + s.charAt(i) - '0';
}
i--;
} else if (c == '-' || c == '+') { // 如果是 + - 优先级比较低的操作,可以把前面都算了
preNum = operate(preNum, num, preSign);
preSign = c;
} else if (c == '/' || c == '*') {
if (preSign == '/' || preSign == '*') {
preNum = operate(preNum, num, preSign);
} else {
arr.add(preNum);
preNum = num;
if (preSign == '-') {
preNum = -num;
}
}
preSign = c;
}
}
preNum = operate(preNum, num, preSign);
for (int val : arr) {
preNum += val;
}
return preNum;
}
private int operate(int a, int b, char op) {
if (op == '-') {
return a - b;
}
if (op == '+') {
return a + b;
}
if (op == '/') {
return a / b;
}
return a * b;
}
// 3ms
// 数组实现栈,不用出栈直接做乘除操作
public int calculate1(String s) {
// public int calculate(String s) {
int res = 0;
char[] cs = s.toCharArray();
int n = cs.length;
int[] stack = new int[n];
int top = -1;
int num = 0;
char operator = '+';
for (int i = 0; i < n; ++i) {
char c = cs[i];
if ('0' <= c && c <= '9') {
num = num * 10 + c - '0';
}
if ((c < '0' && c != ' ') || i == n - 1) {
switch (operator) {
case '+':
stack[++top] = num;
break;
case '-':
stack[++top] = -num;
break;
case '*':
stack[top] *= num;
break;
default:
stack[top] /= num;
}
operator = c;
num = 0;
}
}
while (top != -1) {
res += stack[top--];
}
return res;
}
}