Skip to content

Commit 90a1126

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0227
1 parent 3304a40 commit 90a1126

File tree

6 files changed

+177
-48
lines changed

6 files changed

+177
-48
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
- [逆波兰表达式求值](/solution/0100-0199/0150.Evaluate%20Reverse%20Polish%20Notation/README.md)
136136
- [最近的请求次数](/solution/0900-0999/0933.Number%20of%20Recent%20Calls/README.md)
137137
- [每日温度](/solution/0700-0799/0739.Daily%20Temperatures/README.md)
138+
- [基本计算器 II](/solution/0200-0299/0227.Basic%20Calculator%20II/README.md)
138139

139140
### 动态规划
140141

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
128128
- [Evaluate Reverse Polish Notation](/solution/0100-0199/0150.Evaluate%20Reverse%20Polish%20Notation/README_EN.md)
129129
- [Number of Recent Calls](/solution/0900-0999/0933.Number%20of%20Recent%20Calls/README_EN.md)
130130
- [Daily Temperatures](/solution/0700-0799/0739.Daily%20Temperatures/README_EN.md)
131+
- [Basic Calculator II](/solution/0200-0299/0227.Basic%20Calculator%20II/README_EN.md)
131132

132133
### Dynamic Programming
133134

solution/0200-0299/0227.Basic Calculator II/README.md

+65-2
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,85 @@
3737

3838
<!-- 这里可写通用的实现逻辑 -->
3939

40+
遍历字符串 s,并用变量 `preSign` 记录每个数字之前的运算符,对于第一个数字,其之前的运算符视为加号。每次遍历到数字末尾时,根据 `preSign` 来决定计算方式:
41+
42+
- 加号:将数字压入栈;
43+
- 减号:将数字的相反数压入栈;
44+
- 乘除号:计算数字与栈顶元素,并将栈顶元素替换为计算结果。
45+
4046
<!-- tabs:start -->
4147

4248
### **Python3**
4349

4450
<!-- 这里可写当前语言的特殊实现逻辑 -->
4551

4652
```python
47-
53+
class Solution:
54+
def calculate(self, s: str) -> int:
55+
num, n = 0, len(s)
56+
pre_sign = '+'
57+
stack = []
58+
for i in range(n):
59+
if s[i].isdigit():
60+
num = num * 10 + int(s[i])
61+
if i == n - 1 or (not s[i].isdigit() and s[i] != ' '):
62+
if pre_sign == '+':
63+
stack.append(num)
64+
elif pre_sign == '-':
65+
stack.append(-num)
66+
elif pre_sign == '*':
67+
stack.append(stack.pop() * num)
68+
else:
69+
stack.append(int(stack.pop() / num))
70+
pre_sign = s[i]
71+
num = 0
72+
res = 0
73+
while stack:
74+
res += stack.pop()
75+
return res
4876
```
4977

5078
### **Java**
5179

5280
<!-- 这里可写当前语言的特殊实现逻辑 -->
5381

5482
```java
55-
83+
class Solution {
84+
public int calculate(String s) {
85+
int num = 0;
86+
char preSign = '+';
87+
Deque<Integer> stack = new ArrayDeque<>();
88+
for (int i = 0, n = s.length(); i < n; ++i) {
89+
if (Character.isDigit(s.charAt(i))) {
90+
num = num * 10 + (s.charAt(i) - '0');
91+
}
92+
if (i == n - 1 || (!Character.isDigit(s.charAt(i)) && s.charAt(i) != ' ')) {
93+
switch (preSign) {
94+
case '+':
95+
stack.push(num);
96+
break;
97+
case '-':
98+
stack.push(-num);
99+
break;
100+
case '*':
101+
stack.push(stack.pop() * num);
102+
break;
103+
case '/':
104+
stack.push(stack.pop() / num);
105+
break;
106+
}
107+
preSign = s.charAt(i);
108+
num = 0;
109+
}
110+
}
111+
112+
int res = 0;
113+
while (!stack.isEmpty()) {
114+
res += stack.pop();
115+
}
116+
return res;
117+
}
118+
}
56119
```
57120

58121
### **...**

solution/0200-0299/0227.Basic Calculator II/README_EN.md

+59-2
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,70 @@
5050
### **Python3**
5151

5252
```python
53-
53+
class Solution:
54+
def calculate(self, s: str) -> int:
55+
num, n = 0, len(s)
56+
pre_sign = '+'
57+
stack = []
58+
for i in range(n):
59+
if s[i].isdigit():
60+
num = num * 10 + int(s[i])
61+
if i == n - 1 or (not s[i].isdigit() and s[i] != ' '):
62+
if pre_sign == '+':
63+
stack.append(num)
64+
elif pre_sign == '-':
65+
stack.append(-num)
66+
elif pre_sign == '*':
67+
stack.append(stack.pop() * num)
68+
else:
69+
stack.append(int(stack.pop() / num))
70+
pre_sign = s[i]
71+
num = 0
72+
res = 0
73+
while stack:
74+
res += stack.pop()
75+
return res
5476
```
5577

5678
### **Java**
5779

5880
```java
59-
81+
class Solution {
82+
public int calculate(String s) {
83+
int num = 0;
84+
char preSign = '+';
85+
Deque<Integer> stack = new ArrayDeque<>();
86+
for (int i = 0, n = s.length(); i < n; ++i) {
87+
if (Character.isDigit(s.charAt(i))) {
88+
num = num * 10 + (s.charAt(i) - '0');
89+
}
90+
if (i == n - 1 || (!Character.isDigit(s.charAt(i)) && s.charAt(i) != ' ')) {
91+
switch (preSign) {
92+
case '+':
93+
stack.push(num);
94+
break;
95+
case '-':
96+
stack.push(-num);
97+
break;
98+
case '*':
99+
stack.push(stack.pop() * num);
100+
break;
101+
case '/':
102+
stack.push(stack.pop() / num);
103+
break;
104+
}
105+
preSign = s.charAt(i);
106+
num = 0;
107+
}
108+
}
109+
110+
int res = 0;
111+
while (!stack.isEmpty()) {
112+
res += stack.pop();
113+
}
114+
return res;
115+
}
116+
}
60117
```
61118

62119
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,36 @@
11
class Solution {
22
public int calculate(String s) {
3-
char[] cs = s.toCharArray();
4-
Deque<Character> op = new ArrayDeque<>();
5-
Deque<Integer> num = new ArrayDeque<>();
6-
for (int i = 0; i < cs.length; ++i) {
7-
if (cs[i] == '*' || cs[i] == '/') {
8-
op.push(cs[i]);
9-
} else if (cs[i] == '+' || cs[i] == '-') {
10-
if (!op.isEmpty()) {
11-
calc(op, num);
12-
}
13-
op.push(cs[i]);
14-
} else if (Character.isDigit(cs[i])) {
15-
int j = i;
16-
int k = 0;
17-
while (j < cs.length && Character.isDigit(cs[j])) {
18-
k = k * 10 + cs[j] - '0';
19-
++j;
20-
}
21-
i = j - 1;
22-
num.push(k);
23-
if (!op.isEmpty() && (op.peek() == '*' || op.peek() == '/')) {
24-
calc(op, num);
3+
int num = 0;
4+
char preSign = '+';
5+
Deque<Integer> stack = new ArrayDeque<>();
6+
for (int i = 0, n = s.length(); i < n; ++i) {
7+
if (Character.isDigit(s.charAt(i))) {
8+
num = num * 10 + (s.charAt(i) - '0');
9+
}
10+
if (i == n - 1 || (!Character.isDigit(s.charAt(i)) && s.charAt(i) != ' ')) {
11+
switch (preSign) {
12+
case '+':
13+
stack.push(num);
14+
break;
15+
case '-':
16+
stack.push(-num);
17+
break;
18+
case '*':
19+
stack.push(stack.pop() * num);
20+
break;
21+
case '/':
22+
stack.push(stack.pop() / num);
23+
break;
2524
}
25+
preSign = s.charAt(i);
26+
num = 0;
2627
}
2728
}
28-
if (!op.isEmpty()) {
29-
calc(op, num);
30-
}
31-
return num.peek();
32-
}
3329

34-
private void calc(Deque<Character> op, Deque<Integer> num) {
35-
int y = num.pop();
36-
int x = num.pop();
37-
switch (op.pop()) {
38-
case '*':
39-
num.push(x * y);
40-
break;
41-
case '/':
42-
num.push(x / y);
43-
break;
44-
case '+':
45-
num.push(x + y);
46-
break;
47-
default:
48-
num.push(x - y);
49-
break;
30+
int res = 0;
31+
while (!stack.isEmpty()) {
32+
res += stack.pop();
5033
}
34+
return res;
5135
}
52-
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def calculate(self, s: str) -> int:
3+
num, n = 0, len(s)
4+
pre_sign = '+'
5+
stack = []
6+
for i in range(n):
7+
if s[i].isdigit():
8+
num = num * 10 + int(s[i])
9+
if i == n - 1 or (not s[i].isdigit() and s[i] != ' '):
10+
if pre_sign == '+':
11+
stack.append(num)
12+
elif pre_sign == '-':
13+
stack.append(-num)
14+
elif pre_sign == '*':
15+
stack.append(stack.pop() * num)
16+
else:
17+
stack.append(int(stack.pop() / num))
18+
pre_sign = s[i]
19+
num = 0
20+
res = 0
21+
while stack:
22+
res += stack.pop()
23+
return res

0 commit comments

Comments
 (0)