Skip to content

Commit 064732c

Browse files
committedDec 13, 2021
feat: add solutions to lc problem: No.0682
No.0682.Baseball Game
1 parent 27af1a0 commit 064732c

File tree

6 files changed

+202
-70
lines changed

6 files changed

+202
-70
lines changed
 

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

+71-24
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,11 @@
7070
<li>对于 <code>"C"</code> 和 <code>"D"</code> 操作,题目数据保证记录此操作时前面总是存在一个有效的分数</li>
7171
</ul>
7272

73-
7473
## 解法
7574

7675
<!-- 这里可写通用的实现逻辑 -->
7776

78-
栈实现
77+
利用栈简单模拟即可
7978

8079
<!-- tabs:start -->
8180

@@ -86,17 +85,17 @@
8685
```python
8786
class Solution:
8887
def calPoints(self, ops: List[str]) -> int:
89-
stack = []
88+
stk = []
9089
for op in ops:
91-
if op == 'C':
92-
stack.pop()
90+
if op == '+':
91+
stk.append(stk[-1] + stk[-2])
9392
elif op == 'D':
94-
stack.append(stack[-1] << 1)
95-
elif op == '+':
96-
stack.append(stack[-1] + stack[-2])
93+
stk.append(stk[-1] << 1)
94+
elif op == 'C':
95+
stk.pop()
9796
else:
98-
stack.append(int(op))
99-
return sum(stack)
97+
stk.append(int(op))
98+
return sum(stk)
10099
```
101100

102101
### **Java**
@@ -106,27 +105,75 @@ class Solution:
106105
```java
107106
class Solution {
108107
public int calPoints(String[] ops) {
109-
Deque<Integer> stack = new ArrayDeque<>();
108+
Deque<Integer> stk = new ArrayDeque<>();
110109
for (String op : ops) {
111-
if ("C".equals(op)) {
112-
stack.pop();
110+
if ("+".equals(op)) {
111+
int a = stk.pop();
112+
int b = stk.peek();
113+
stk.push(a);
114+
stk.push(a + b);
113115
} else if ("D".equals(op)) {
114-
stack.push(stack.peek() << 1);
115-
} else if ("+".equals(op)) {
116-
Integer a = stack.pop();
117-
Integer b = stack.peek();
118-
stack.push(a);
119-
stack.push(a + b);
116+
stk.push(stk.peek() << 1);
117+
} else if ("C".equals(op)) {
118+
stk.pop();
120119
} else {
121-
stack.push(Integer.valueOf(op));
120+
stk.push(Integer.valueOf(op));
122121
}
123122
}
124-
int res = 0;
125-
for (Integer score : stack) {
126-
res += score;
123+
return stk.stream().mapToInt(Integer::intValue).sum();
124+
}
125+
}
126+
```
127+
128+
### **C++**
129+
130+
```cpp
131+
class Solution {
132+
public:
133+
int calPoints(vector<string>& ops) {
134+
vector<int> stk;
135+
for (auto& op : ops)
136+
{
137+
int n = stk.size();
138+
if (op == "+")
139+
{
140+
int a = stk[n - 1];
141+
int b = stk[n - 2];
142+
stk.push_back(a + b);
143+
}
144+
else if (op == "D") stk.push_back(stk[n - 1] * 2);
145+
else if (op == "C") stk.pop_back();
146+
else stk.push_back(stoi(op));
127147
}
128-
return res;
148+
return accumulate(stk.begin(), stk.end(), 0);
129149
}
150+
};
151+
```
152+
153+
### **Go**
154+
155+
```go
156+
func calPoints(ops []string) int {
157+
var stk []int
158+
for _, op := range ops {
159+
n := len(stk)
160+
switch op {
161+
case "+":
162+
stk = append(stk, stk[n-1]+stk[n-2])
163+
case "D":
164+
stk = append(stk, stk[n-1]*2)
165+
case "C":
166+
stk = stk[:n-1]
167+
default:
168+
num, _ := strconv.Atoi(op)
169+
stk = append(stk, num)
170+
}
171+
}
172+
ans := 0
173+
for _, score := range stk {
174+
ans += score
175+
}
176+
return ans
130177
}
131178
```
132179

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

+70-23
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.
6666
<li>For operations <code>&quot;C&quot;</code> and <code>&quot;D&quot;</code>, there will always be at least one previous score on the record.</li>
6767
</ul>
6868

69-
7069
## Solutions
7170

7271
<!-- tabs:start -->
@@ -76,45 +75,93 @@ The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.
7675
```python
7776
class Solution:
7877
def calPoints(self, ops: List[str]) -> int:
79-
stack = []
78+
stk = []
8079
for op in ops:
81-
if op == 'C':
82-
stack.pop()
80+
if op == '+':
81+
stk.append(stk[-1] + stk[-2])
8382
elif op == 'D':
84-
stack.append(stack[-1] << 1)
85-
elif op == '+':
86-
stack.append(stack[-1] + stack[-2])
83+
stk.append(stk[-1] << 1)
84+
elif op == 'C':
85+
stk.pop()
8786
else:
88-
stack.append(int(op))
89-
return sum(stack)
87+
stk.append(int(op))
88+
return sum(stk)
9089
```
9190

9291
### **Java**
9392

9493
```java
9594
class Solution {
9695
public int calPoints(String[] ops) {
97-
Deque<Integer> stack = new ArrayDeque<>();
96+
Deque<Integer> stk = new ArrayDeque<>();
9897
for (String op : ops) {
99-
if ("C".equals(op)) {
100-
stack.pop();
98+
if ("+".equals(op)) {
99+
int a = stk.pop();
100+
int b = stk.peek();
101+
stk.push(a);
102+
stk.push(a + b);
101103
} else if ("D".equals(op)) {
102-
stack.push(stack.peek() << 1);
103-
} else if ("+".equals(op)) {
104-
Integer a = stack.pop();
105-
Integer b = stack.peek();
106-
stack.push(a);
107-
stack.push(a + b);
104+
stk.push(stk.peek() << 1);
105+
} else if ("C".equals(op)) {
106+
stk.pop();
108107
} else {
109-
stack.push(Integer.valueOf(op));
108+
stk.push(Integer.valueOf(op));
110109
}
111110
}
112-
int res = 0;
113-
for (Integer score : stack) {
114-
res += score;
111+
return stk.stream().mapToInt(Integer::intValue).sum();
112+
}
113+
}
114+
```
115+
116+
### **C++**
117+
118+
```cpp
119+
class Solution {
120+
public:
121+
int calPoints(vector<string>& ops) {
122+
vector<int> stk;
123+
for (auto& op : ops)
124+
{
125+
int n = stk.size();
126+
if (op == "+")
127+
{
128+
int a = stk[n - 1];
129+
int b = stk[n - 2];
130+
stk.push_back(a + b);
131+
}
132+
else if (op == "D") stk.push_back(stk[n - 1] * 2);
133+
else if (op == "C") stk.pop_back();
134+
else stk.push_back(stoi(op));
115135
}
116-
return res;
136+
return accumulate(stk.begin(), stk.end(), 0);
117137
}
138+
};
139+
```
140+
141+
### **Go**
142+
143+
```go
144+
func calPoints(ops []string) int {
145+
var stk []int
146+
for _, op := range ops {
147+
n := len(stk)
148+
switch op {
149+
case "+":
150+
stk = append(stk, stk[n-1]+stk[n-2])
151+
case "D":
152+
stk = append(stk, stk[n-1]*2)
153+
case "C":
154+
stk = stk[:n-1]
155+
default:
156+
num, _ := strconv.Atoi(op)
157+
stk = append(stk, num)
158+
}
159+
}
160+
ans := 0
161+
for _, score := range stk {
162+
ans += score
163+
}
164+
return ans
118165
}
119166
```
120167

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int calPoints(vector<string>& ops) {
4+
vector<int> stk;
5+
for (auto& op : ops)
6+
{
7+
int n = stk.size();
8+
if (op == "+")
9+
{
10+
int a = stk[n - 1];
11+
int b = stk[n - 2];
12+
stk.push_back(a + b);
13+
}
14+
else if (op == "D") stk.push_back(stk[n - 1] * 2);
15+
else if (op == "C") stk.pop_back();
16+
else stk.push_back(stoi(op));
17+
}
18+
return accumulate(stk.begin(), stk.end(), 0);
19+
}
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
func calPoints(ops []string) int {
2+
var stk []int
3+
for _, op := range ops {
4+
n := len(stk)
5+
switch op {
6+
case "+":
7+
stk = append(stk, stk[n-1]+stk[n-2])
8+
case "D":
9+
stk = append(stk, stk[n-1]*2)
10+
case "C":
11+
stk = stk[:n-1]
12+
default:
13+
num, _ := strconv.Atoi(op)
14+
stk = append(stk, num)
15+
}
16+
}
17+
ans := 0
18+
for _, score := range stk {
19+
ans += score
20+
}
21+
return ans
22+
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
class Solution {
22
public int calPoints(String[] ops) {
3-
Deque<Integer> stack = new ArrayDeque<>();
3+
Deque<Integer> stk = new ArrayDeque<>();
44
for (String op : ops) {
5-
if ("C".equals(op)) {
6-
stack.pop();
5+
if ("+".equals(op)) {
6+
int a = stk.pop();
7+
int b = stk.peek();
8+
stk.push(a);
9+
stk.push(a + b);
710
} 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);
11+
stk.push(stk.peek() << 1);
12+
} else if ("C".equals(op)) {
13+
stk.pop();
1414
} else {
15-
stack.push(Integer.valueOf(op));
15+
stk.push(Integer.valueOf(op));
1616
}
1717
}
18-
int res = 0;
19-
for (Integer score : stack) {
20-
res += score;
21-
}
22-
return res;
18+
return stk.stream().mapToInt(Integer::intValue).sum();
2319
}
2420
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Solution:
22
def calPoints(self, ops: List[str]) -> int:
3-
stack = []
3+
stk = []
44
for op in ops:
5-
if op == 'C':
6-
stack.pop()
5+
if op == '+':
6+
stk.append(stk[-1] + stk[-2])
77
elif op == 'D':
8-
stack.append(stack[-1] << 1)
9-
elif op == '+':
10-
stack.append(stack[-1] + stack[-2])
8+
stk.append(stk[-1] << 1)
9+
elif op == 'C':
10+
stk.pop()
1111
else:
12-
stack.append(int(op))
13-
return sum(stack)
12+
stk.append(int(op))
13+
return sum(stk)

0 commit comments

Comments
 (0)