Skip to content

Commit aa4e2e0

Browse files
authored
feat: update solutions to lc problem: No.0682 (#3333)
No.0682.Baseball Game
1 parent e42bd8a commit aa4e2e0

File tree

8 files changed

+139
-122
lines changed

8 files changed

+139
-122
lines changed

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

+51-41
Original file line numberDiff line numberDiff line change
@@ -88,22 +88,35 @@ tags:
8888

8989
<!-- solution:start -->
9090

91-
### 方法一
91+
### 方法一:栈 + 模拟
92+
93+
我们可以使用栈来模拟这个过程。
94+
95+
遍历 $\textit{operations}$,对于每个操作:
96+
97+
- 如果是 `+`,则将栈顶两个元素相加,然后将结果入栈;
98+
- 如果是 `D`,则将栈顶元素的值乘以 2,然后将结果入栈;
99+
- 如果是 `C`,则将栈顶元素出栈;
100+
- 如果是数字,将数字入栈。
101+
102+
最后,将栈中的所有元素求和即为答案。
103+
104+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 $\textit{operations}$ 的长度。
92105

93106
<!-- tabs:start -->
94107

95108
#### Python3
96109

97110
```python
98111
class Solution:
99-
def calPoints(self, ops: List[str]) -> int:
112+
def calPoints(self, operations: List[str]) -> int:
100113
stk = []
101-
for op in ops:
102-
if op == '+':
114+
for op in operations:
115+
if op == "+":
103116
stk.append(stk[-1] + stk[-2])
104-
elif op == 'D':
117+
elif op == "D":
105118
stk.append(stk[-1] << 1)
106-
elif op == 'C':
119+
elif op == "C":
107120
stk.pop()
108121
else:
109122
stk.append(int(op))
@@ -114,9 +127,9 @@ class Solution:
114127

115128
```java
116129
class Solution {
117-
public int calPoints(String[] ops) {
130+
public int calPoints(String[] operations) {
118131
Deque<Integer> stk = new ArrayDeque<>();
119-
for (String op : ops) {
132+
for (String op : operations) {
120133
if ("+".equals(op)) {
121134
int a = stk.pop();
122135
int b = stk.peek();
@@ -140,20 +153,19 @@ class Solution {
140153
```cpp
141154
class Solution {
142155
public:
143-
int calPoints(vector<string>& ops) {
156+
int calPoints(vector<string>& operations) {
144157
vector<int> stk;
145-
for (auto& op : ops) {
158+
for (auto& op : operations) {
146159
int n = stk.size();
147160
if (op == "+") {
148-
int a = stk[n - 1];
149-
int b = stk[n - 2];
150-
stk.push_back(a + b);
151-
} else if (op == "D")
152-
stk.push_back(stk[n - 1] * 2);
153-
else if (op == "C")
161+
stk.push_back(stk[n - 1] + stk[n - 2]);
162+
} else if (op == "D") {
163+
stk.push_back(stk[n - 1] << 1);
164+
} else if (op == "C") {
154165
stk.pop_back();
155-
else
166+
} else {
156167
stk.push_back(stoi(op));
168+
}
157169
}
158170
return accumulate(stk.begin(), stk.end(), 0);
159171
}
@@ -163,9 +175,9 @@ public:
163175
#### Go
164176
165177
```go
166-
func calPoints(ops []string) int {
178+
func calPoints(operations []string) (ans int) {
167179
var stk []int
168-
for _, op := range ops {
180+
for _, op := range operations {
169181
n := len(stk)
170182
switch op {
171183
case "+":
@@ -179,59 +191,57 @@ func calPoints(ops []string) int {
179191
stk = append(stk, num)
180192
}
181193
}
182-
ans := 0
183-
for _, score := range stk {
184-
ans += score
194+
for _, x := range stk {
195+
ans += x
185196
}
186-
return ans
197+
return
187198
}
188199
```
189200

190201
#### TypeScript
191202

192203
```ts
193-
function calPoints(ops: string[]): number {
194-
const stack = [];
195-
for (const op of ops) {
196-
const n = stack.length;
204+
function calPoints(operations: string[]): number {
205+
const stk: number[] = [];
206+
for (const op of operations) {
197207
if (op === '+') {
198-
stack.push(stack[n - 1] + stack[n - 2]);
208+
stk.push(stk.at(-1)! + stk.at(-2)!);
199209
} else if (op === 'D') {
200-
stack.push(stack[n - 1] * 2);
210+
stk.push(stk.at(-1)! << 1);
201211
} else if (op === 'C') {
202-
stack.pop();
212+
stk.pop();
203213
} else {
204-
stack.push(Number(op));
214+
stk.push(+op);
205215
}
206216
}
207-
return stack.reduce((p, v) => p + v);
217+
return stk.reduce((a, b) => a + b, 0);
208218
}
209219
```
210220

211221
#### Rust
212222

213223
```rust
214224
impl Solution {
215-
pub fn cal_points(ops: Vec<String>) -> i32 {
216-
let mut stack = vec![];
217-
for op in ops {
225+
pub fn cal_points(operations: Vec<String>) -> i32 {
226+
let mut stk = vec![];
227+
for op in operations {
218228
match op.as_str() {
219229
"+" => {
220-
let n = stack.len();
221-
stack.push(stack[n - 1] + stack[n - 2]);
230+
let n = stk.len();
231+
stk.push(stk[n - 1] + stk[n - 2]);
222232
}
223233
"D" => {
224-
stack.push(stack.last().unwrap() * 2);
234+
stk.push(stk.last().unwrap() * 2);
225235
}
226236
"C" => {
227-
stack.pop();
237+
stk.pop();
228238
}
229239
n => {
230-
stack.push(n.parse::<i32>().unwrap());
240+
stk.push(n.parse::<i32>().unwrap());
231241
}
232242
}
233243
}
234-
stack.into_iter().sum()
244+
stk.into_iter().sum()
235245
}
236246
}
237247
```

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

+51-41
Original file line numberDiff line numberDiff line change
@@ -110,22 +110,35 @@ Since the record is empty, the total sum is 0.
110110

111111
<!-- solution:start -->
112112

113-
### Solution 1
113+
### Solution 1: Stack + Simulation
114+
115+
We can use a stack to simulate this process.
116+
117+
Traverse $\textit{operations}$, for each operation:
118+
119+
- If it is `+`, add the top two elements of the stack and push the result onto the stack;
120+
- If it is `D`, multiply the top element of the stack by 2 and push the result onto the stack;
121+
- If it is `C`, pop the top element of the stack;
122+
- If it is a number, push the number onto the stack.
123+
124+
Finally, sum all the elements in the stack to get the answer.
125+
126+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of $\textit{operations}$.
114127

115128
<!-- tabs:start -->
116129

117130
#### Python3
118131

119132
```python
120133
class Solution:
121-
def calPoints(self, ops: List[str]) -> int:
134+
def calPoints(self, operations: List[str]) -> int:
122135
stk = []
123-
for op in ops:
124-
if op == '+':
136+
for op in operations:
137+
if op == "+":
125138
stk.append(stk[-1] + stk[-2])
126-
elif op == 'D':
139+
elif op == "D":
127140
stk.append(stk[-1] << 1)
128-
elif op == 'C':
141+
elif op == "C":
129142
stk.pop()
130143
else:
131144
stk.append(int(op))
@@ -136,9 +149,9 @@ class Solution:
136149

137150
```java
138151
class Solution {
139-
public int calPoints(String[] ops) {
152+
public int calPoints(String[] operations) {
140153
Deque<Integer> stk = new ArrayDeque<>();
141-
for (String op : ops) {
154+
for (String op : operations) {
142155
if ("+".equals(op)) {
143156
int a = stk.pop();
144157
int b = stk.peek();
@@ -162,20 +175,19 @@ class Solution {
162175
```cpp
163176
class Solution {
164177
public:
165-
int calPoints(vector<string>& ops) {
178+
int calPoints(vector<string>& operations) {
166179
vector<int> stk;
167-
for (auto& op : ops) {
180+
for (auto& op : operations) {
168181
int n = stk.size();
169182
if (op == "+") {
170-
int a = stk[n - 1];
171-
int b = stk[n - 2];
172-
stk.push_back(a + b);
173-
} else if (op == "D")
174-
stk.push_back(stk[n - 1] * 2);
175-
else if (op == "C")
183+
stk.push_back(stk[n - 1] + stk[n - 2]);
184+
} else if (op == "D") {
185+
stk.push_back(stk[n - 1] << 1);
186+
} else if (op == "C") {
176187
stk.pop_back();
177-
else
188+
} else {
178189
stk.push_back(stoi(op));
190+
}
179191
}
180192
return accumulate(stk.begin(), stk.end(), 0);
181193
}
@@ -185,9 +197,9 @@ public:
185197
#### Go
186198
187199
```go
188-
func calPoints(ops []string) int {
200+
func calPoints(operations []string) (ans int) {
189201
var stk []int
190-
for _, op := range ops {
202+
for _, op := range operations {
191203
n := len(stk)
192204
switch op {
193205
case "+":
@@ -201,59 +213,57 @@ func calPoints(ops []string) int {
201213
stk = append(stk, num)
202214
}
203215
}
204-
ans := 0
205-
for _, score := range stk {
206-
ans += score
216+
for _, x := range stk {
217+
ans += x
207218
}
208-
return ans
219+
return
209220
}
210221
```
211222

212223
#### TypeScript
213224

214225
```ts
215-
function calPoints(ops: string[]): number {
216-
const stack = [];
217-
for (const op of ops) {
218-
const n = stack.length;
226+
function calPoints(operations: string[]): number {
227+
const stk: number[] = [];
228+
for (const op of operations) {
219229
if (op === '+') {
220-
stack.push(stack[n - 1] + stack[n - 2]);
230+
stk.push(stk.at(-1)! + stk.at(-2)!);
221231
} else if (op === 'D') {
222-
stack.push(stack[n - 1] * 2);
232+
stk.push(stk.at(-1)! << 1);
223233
} else if (op === 'C') {
224-
stack.pop();
234+
stk.pop();
225235
} else {
226-
stack.push(Number(op));
236+
stk.push(+op);
227237
}
228238
}
229-
return stack.reduce((p, v) => p + v);
239+
return stk.reduce((a, b) => a + b, 0);
230240
}
231241
```
232242

233243
#### Rust
234244

235245
```rust
236246
impl Solution {
237-
pub fn cal_points(ops: Vec<String>) -> i32 {
238-
let mut stack = vec![];
239-
for op in ops {
247+
pub fn cal_points(operations: Vec<String>) -> i32 {
248+
let mut stk = vec![];
249+
for op in operations {
240250
match op.as_str() {
241251
"+" => {
242-
let n = stack.len();
243-
stack.push(stack[n - 1] + stack[n - 2]);
252+
let n = stk.len();
253+
stk.push(stk[n - 1] + stk[n - 2]);
244254
}
245255
"D" => {
246-
stack.push(stack.last().unwrap() * 2);
256+
stk.push(stk.last().unwrap() * 2);
247257
}
248258
"C" => {
249-
stack.pop();
259+
stk.pop();
250260
}
251261
n => {
252-
stack.push(n.parse::<i32>().unwrap());
262+
stk.push(n.parse::<i32>().unwrap());
253263
}
254264
}
255265
}
256-
stack.into_iter().sum()
266+
stk.into_iter().sum()
257267
}
258268
}
259269
```

solution/0600-0699/0682.Baseball Game/Solution.cpp

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
class Solution {
22
public:
3-
int calPoints(vector<string>& ops) {
3+
int calPoints(vector<string>& operations) {
44
vector<int> stk;
5-
for (auto& op : ops) {
5+
for (auto& op : operations) {
66
int n = stk.size();
77
if (op == "+") {
8-
int a = stk[n - 1];
9-
int b = stk[n - 2];
10-
stk.push_back(a + b);
11-
} else if (op == "D")
12-
stk.push_back(stk[n - 1] * 2);
13-
else if (op == "C")
8+
stk.push_back(stk[n - 1] + stk[n - 2]);
9+
} else if (op == "D") {
10+
stk.push_back(stk[n - 1] << 1);
11+
} else if (op == "C") {
1412
stk.pop_back();
15-
else
13+
} else {
1614
stk.push_back(stoi(op));
15+
}
1716
}
1817
return accumulate(stk.begin(), stk.end(), 0);
1918
}

0 commit comments

Comments
 (0)