Skip to content

Commit ca9bcb0

Browse files
committed
feat: add java and python solutions to leetcode problem: No.0682
添加 LeetCode No.0682 题解
1 parent dd9d325 commit ca9bcb0

File tree

4 files changed

+113
-4
lines changed

4 files changed

+113
-4
lines changed

solution/0600-0699/0682.Baseball Game/README.md

+39-2
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,59 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
栈实现。
57+
5658
<!-- tabs:start -->
5759

5860
### **Python3**
5961

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

6264
```python
63-
65+
class Solution:
66+
def calPoints(self, ops: List[str]) -> int:
67+
stack = []
68+
for op in ops:
69+
if op == 'C':
70+
stack.pop()
71+
elif op == 'D':
72+
stack.append(stack[-1] << 1)
73+
elif op == '+':
74+
stack.append(stack[-1] + stack[-2])
75+
else:
76+
stack.append(int(op))
77+
return sum(stack)
6478
```
6579

6680
### **Java**
6781

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

7084
```java
71-
85+
class Solution {
86+
public int calPoints(String[] ops) {
87+
Deque<Integer> stack = new ArrayDeque<>();
88+
for (String op : ops) {
89+
if ("C".equals(op)) {
90+
stack.pop();
91+
} else if ("D".equals(op)) {
92+
stack.push(stack.peek() << 1);
93+
} else if ("+".equals(op)) {
94+
Integer a = stack.pop();
95+
Integer b = stack.peek();
96+
stack.push(a);
97+
stack.push(a + b);
98+
} else {
99+
stack.push(Integer.valueOf(op));
100+
}
101+
}
102+
int res = 0;
103+
for (Integer score : stack) {
104+
res += score;
105+
}
106+
return res;
107+
}
108+
}
72109
```
73110

74111
### **...**

solution/0600-0699/0682.Baseball Game/README_EN.md

+37-2
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,48 @@ Round 7: You could get 9 + 5 = 14 points. The sum is 27.
109109
### **Python3**
110110

111111
```python
112-
112+
class Solution:
113+
def calPoints(self, ops: List[str]) -> int:
114+
stack = []
115+
for op in ops:
116+
if op == 'C':
117+
stack.pop()
118+
elif op == 'D':
119+
stack.append(stack[-1] << 1)
120+
elif op == '+':
121+
stack.append(stack[-1] + stack[-2])
122+
else:
123+
stack.append(int(op))
124+
return sum(stack)
113125
```
114126

115127
### **Java**
116128

117129
```java
118-
130+
class Solution {
131+
public int calPoints(String[] ops) {
132+
Deque<Integer> stack = new ArrayDeque<>();
133+
for (String op : ops) {
134+
if ("C".equals(op)) {
135+
stack.pop();
136+
} else if ("D".equals(op)) {
137+
stack.push(stack.peek() << 1);
138+
} else if ("+".equals(op)) {
139+
Integer a = stack.pop();
140+
Integer b = stack.peek();
141+
stack.push(a);
142+
stack.push(a + b);
143+
} else {
144+
stack.push(Integer.valueOf(op));
145+
}
146+
}
147+
int res = 0;
148+
for (Integer score : stack) {
149+
res += score;
150+
}
151+
return res;
152+
}
153+
}
119154
```
120155

121156
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int calPoints(String[] ops) {
3+
Deque<Integer> stack = new ArrayDeque<>();
4+
for (String op : ops) {
5+
if ("C".equals(op)) {
6+
stack.pop();
7+
} else if ("D".equals(op)) {
8+
stack.push(stack.peek() << 1);
9+
} else if ("+".equals(op)) {
10+
Integer a = stack.pop();
11+
Integer b = stack.peek();
12+
stack.push(a);
13+
stack.push(a + b);
14+
} else {
15+
stack.push(Integer.valueOf(op));
16+
}
17+
}
18+
int res = 0;
19+
for (Integer score : stack) {
20+
res += score;
21+
}
22+
return res;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def calPoints(self, ops: List[str]) -> int:
3+
stack = []
4+
for op in ops:
5+
if op == 'C':
6+
stack.pop()
7+
elif op == 'D':
8+
stack.append(stack[-1] << 1)
9+
elif op == '+':
10+
stack.append(stack[-1] + stack[-2])
11+
else:
12+
stack.append(int(op))
13+
return sum(stack)

0 commit comments

Comments
 (0)