From da5d42f110bfe4ca8067ef8aedd52e1456b5b90a Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 29 Jul 2024 08:36:47 +0800 Subject: [PATCH] feat: update solutions to lc problem: No.0682 No.0682.Baseball Game --- .../0600-0699/0682.Baseball Game/README.md | 92 ++++++++++--------- .../0600-0699/0682.Baseball Game/README_EN.md | 92 ++++++++++--------- .../0600-0699/0682.Baseball Game/Solution.cpp | 17 ++-- .../0600-0699/0682.Baseball Game/Solution.go | 11 +-- .../0682.Baseball Game/Solution.java | 4 +- .../0600-0699/0682.Baseball Game/Solution.py | 10 +- .../0600-0699/0682.Baseball Game/Solution.rs | 18 ++-- .../0600-0699/0682.Baseball Game/Solution.ts | 17 ++-- 8 files changed, 139 insertions(+), 122 deletions(-) diff --git a/solution/0600-0699/0682.Baseball Game/README.md b/solution/0600-0699/0682.Baseball Game/README.md index 307c7ac214555..a7b233a1ac756 100644 --- a/solution/0600-0699/0682.Baseball Game/README.md +++ b/solution/0600-0699/0682.Baseball Game/README.md @@ -88,7 +88,20 @@ tags: -### 方法一 +### 方法一:栈 + 模拟 + +我们可以使用栈来模拟这个过程。 + +遍历 $\textit{operations}$,对于每个操作: + +- 如果是 `+`,则将栈顶两个元素相加,然后将结果入栈; +- 如果是 `D`,则将栈顶元素的值乘以 2,然后将结果入栈; +- 如果是 `C`,则将栈顶元素出栈; +- 如果是数字,将数字入栈。 + +最后,将栈中的所有元素求和即为答案。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 $\textit{operations}$ 的长度。 @@ -96,14 +109,14 @@ tags: ```python class Solution: - def calPoints(self, ops: List[str]) -> int: + def calPoints(self, operations: List[str]) -> int: stk = [] - for op in ops: - if op == '+': + for op in operations: + if op == "+": stk.append(stk[-1] + stk[-2]) - elif op == 'D': + elif op == "D": stk.append(stk[-1] << 1) - elif op == 'C': + elif op == "C": stk.pop() else: stk.append(int(op)) @@ -114,9 +127,9 @@ class Solution: ```java class Solution { - public int calPoints(String[] ops) { + public int calPoints(String[] operations) { Deque stk = new ArrayDeque<>(); - for (String op : ops) { + for (String op : operations) { if ("+".equals(op)) { int a = stk.pop(); int b = stk.peek(); @@ -140,20 +153,19 @@ class Solution { ```cpp class Solution { public: - int calPoints(vector& ops) { + int calPoints(vector& operations) { vector stk; - for (auto& op : ops) { + for (auto& op : operations) { int n = stk.size(); if (op == "+") { - int a = stk[n - 1]; - int b = stk[n - 2]; - stk.push_back(a + b); - } else if (op == "D") - stk.push_back(stk[n - 1] * 2); - else if (op == "C") + stk.push_back(stk[n - 1] + stk[n - 2]); + } else if (op == "D") { + stk.push_back(stk[n - 1] << 1); + } else if (op == "C") { stk.pop_back(); - else + } else { stk.push_back(stoi(op)); + } } return accumulate(stk.begin(), stk.end(), 0); } @@ -163,9 +175,9 @@ public: #### Go ```go -func calPoints(ops []string) int { +func calPoints(operations []string) (ans int) { var stk []int - for _, op := range ops { + for _, op := range operations { n := len(stk) switch op { case "+": @@ -179,32 +191,30 @@ func calPoints(ops []string) int { stk = append(stk, num) } } - ans := 0 - for _, score := range stk { - ans += score + for _, x := range stk { + ans += x } - return ans + return } ``` #### TypeScript ```ts -function calPoints(ops: string[]): number { - const stack = []; - for (const op of ops) { - const n = stack.length; +function calPoints(operations: string[]): number { + const stk: number[] = []; + for (const op of operations) { if (op === '+') { - stack.push(stack[n - 1] + stack[n - 2]); + stk.push(stk.at(-1)! + stk.at(-2)!); } else if (op === 'D') { - stack.push(stack[n - 1] * 2); + stk.push(stk.at(-1)! << 1); } else if (op === 'C') { - stack.pop(); + stk.pop(); } else { - stack.push(Number(op)); + stk.push(+op); } } - return stack.reduce((p, v) => p + v); + return stk.reduce((a, b) => a + b, 0); } ``` @@ -212,26 +222,26 @@ function calPoints(ops: string[]): number { ```rust impl Solution { - pub fn cal_points(ops: Vec) -> i32 { - let mut stack = vec![]; - for op in ops { + pub fn cal_points(operations: Vec) -> i32 { + let mut stk = vec![]; + for op in operations { match op.as_str() { "+" => { - let n = stack.len(); - stack.push(stack[n - 1] + stack[n - 2]); + let n = stk.len(); + stk.push(stk[n - 1] + stk[n - 2]); } "D" => { - stack.push(stack.last().unwrap() * 2); + stk.push(stk.last().unwrap() * 2); } "C" => { - stack.pop(); + stk.pop(); } n => { - stack.push(n.parse::().unwrap()); + stk.push(n.parse::().unwrap()); } } } - stack.into_iter().sum() + stk.into_iter().sum() } } ``` diff --git a/solution/0600-0699/0682.Baseball Game/README_EN.md b/solution/0600-0699/0682.Baseball Game/README_EN.md index 2f043c4daa95d..4fc0adc33aa2b 100644 --- a/solution/0600-0699/0682.Baseball Game/README_EN.md +++ b/solution/0600-0699/0682.Baseball Game/README_EN.md @@ -110,7 +110,20 @@ Since the record is empty, the total sum is 0. -### Solution 1 +### Solution 1: Stack + Simulation + +We can use a stack to simulate this process. + +Traverse $\textit{operations}$, for each operation: + +- If it is `+`, add the top two elements of the stack and push the result onto the stack; +- If it is `D`, multiply the top element of the stack by 2 and push the result onto the stack; +- If it is `C`, pop the top element of the stack; +- If it is a number, push the number onto the stack. + +Finally, sum all the elements in the stack to get the answer. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of $\textit{operations}$. @@ -118,14 +131,14 @@ Since the record is empty, the total sum is 0. ```python class Solution: - def calPoints(self, ops: List[str]) -> int: + def calPoints(self, operations: List[str]) -> int: stk = [] - for op in ops: - if op == '+': + for op in operations: + if op == "+": stk.append(stk[-1] + stk[-2]) - elif op == 'D': + elif op == "D": stk.append(stk[-1] << 1) - elif op == 'C': + elif op == "C": stk.pop() else: stk.append(int(op)) @@ -136,9 +149,9 @@ class Solution: ```java class Solution { - public int calPoints(String[] ops) { + public int calPoints(String[] operations) { Deque stk = new ArrayDeque<>(); - for (String op : ops) { + for (String op : operations) { if ("+".equals(op)) { int a = stk.pop(); int b = stk.peek(); @@ -162,20 +175,19 @@ class Solution { ```cpp class Solution { public: - int calPoints(vector& ops) { + int calPoints(vector& operations) { vector stk; - for (auto& op : ops) { + for (auto& op : operations) { int n = stk.size(); if (op == "+") { - int a = stk[n - 1]; - int b = stk[n - 2]; - stk.push_back(a + b); - } else if (op == "D") - stk.push_back(stk[n - 1] * 2); - else if (op == "C") + stk.push_back(stk[n - 1] + stk[n - 2]); + } else if (op == "D") { + stk.push_back(stk[n - 1] << 1); + } else if (op == "C") { stk.pop_back(); - else + } else { stk.push_back(stoi(op)); + } } return accumulate(stk.begin(), stk.end(), 0); } @@ -185,9 +197,9 @@ public: #### Go ```go -func calPoints(ops []string) int { +func calPoints(operations []string) (ans int) { var stk []int - for _, op := range ops { + for _, op := range operations { n := len(stk) switch op { case "+": @@ -201,32 +213,30 @@ func calPoints(ops []string) int { stk = append(stk, num) } } - ans := 0 - for _, score := range stk { - ans += score + for _, x := range stk { + ans += x } - return ans + return } ``` #### TypeScript ```ts -function calPoints(ops: string[]): number { - const stack = []; - for (const op of ops) { - const n = stack.length; +function calPoints(operations: string[]): number { + const stk: number[] = []; + for (const op of operations) { if (op === '+') { - stack.push(stack[n - 1] + stack[n - 2]); + stk.push(stk.at(-1)! + stk.at(-2)!); } else if (op === 'D') { - stack.push(stack[n - 1] * 2); + stk.push(stk.at(-1)! << 1); } else if (op === 'C') { - stack.pop(); + stk.pop(); } else { - stack.push(Number(op)); + stk.push(+op); } } - return stack.reduce((p, v) => p + v); + return stk.reduce((a, b) => a + b, 0); } ``` @@ -234,26 +244,26 @@ function calPoints(ops: string[]): number { ```rust impl Solution { - pub fn cal_points(ops: Vec) -> i32 { - let mut stack = vec![]; - for op in ops { + pub fn cal_points(operations: Vec) -> i32 { + let mut stk = vec![]; + for op in operations { match op.as_str() { "+" => { - let n = stack.len(); - stack.push(stack[n - 1] + stack[n - 2]); + let n = stk.len(); + stk.push(stk[n - 1] + stk[n - 2]); } "D" => { - stack.push(stack.last().unwrap() * 2); + stk.push(stk.last().unwrap() * 2); } "C" => { - stack.pop(); + stk.pop(); } n => { - stack.push(n.parse::().unwrap()); + stk.push(n.parse::().unwrap()); } } } - stack.into_iter().sum() + stk.into_iter().sum() } } ``` diff --git a/solution/0600-0699/0682.Baseball Game/Solution.cpp b/solution/0600-0699/0682.Baseball Game/Solution.cpp index 94b8acc343857..16c9050e13c07 100644 --- a/solution/0600-0699/0682.Baseball Game/Solution.cpp +++ b/solution/0600-0699/0682.Baseball Game/Solution.cpp @@ -1,19 +1,18 @@ class Solution { public: - int calPoints(vector& ops) { + int calPoints(vector& operations) { vector stk; - for (auto& op : ops) { + for (auto& op : operations) { int n = stk.size(); if (op == "+") { - int a = stk[n - 1]; - int b = stk[n - 2]; - stk.push_back(a + b); - } else if (op == "D") - stk.push_back(stk[n - 1] * 2); - else if (op == "C") + stk.push_back(stk[n - 1] + stk[n - 2]); + } else if (op == "D") { + stk.push_back(stk[n - 1] << 1); + } else if (op == "C") { stk.pop_back(); - else + } else { stk.push_back(stoi(op)); + } } return accumulate(stk.begin(), stk.end(), 0); } diff --git a/solution/0600-0699/0682.Baseball Game/Solution.go b/solution/0600-0699/0682.Baseball Game/Solution.go index 888e86f3f7956..d894f2d4e309d 100644 --- a/solution/0600-0699/0682.Baseball Game/Solution.go +++ b/solution/0600-0699/0682.Baseball Game/Solution.go @@ -1,6 +1,6 @@ -func calPoints(ops []string) int { +func calPoints(operations []string) (ans int) { var stk []int - for _, op := range ops { + for _, op := range operations { n := len(stk) switch op { case "+": @@ -14,9 +14,8 @@ func calPoints(ops []string) int { stk = append(stk, num) } } - ans := 0 - for _, score := range stk { - ans += score + for _, x := range stk { + ans += x } - return ans + return } \ No newline at end of file diff --git a/solution/0600-0699/0682.Baseball Game/Solution.java b/solution/0600-0699/0682.Baseball Game/Solution.java index 17e0705bdb06f..dc31da6f4c1e5 100644 --- a/solution/0600-0699/0682.Baseball Game/Solution.java +++ b/solution/0600-0699/0682.Baseball Game/Solution.java @@ -1,7 +1,7 @@ class Solution { - public int calPoints(String[] ops) { + public int calPoints(String[] operations) { Deque stk = new ArrayDeque<>(); - for (String op : ops) { + for (String op : operations) { if ("+".equals(op)) { int a = stk.pop(); int b = stk.peek(); diff --git a/solution/0600-0699/0682.Baseball Game/Solution.py b/solution/0600-0699/0682.Baseball Game/Solution.py index e7d3594927991..124ace608b1cb 100644 --- a/solution/0600-0699/0682.Baseball Game/Solution.py +++ b/solution/0600-0699/0682.Baseball Game/Solution.py @@ -1,12 +1,12 @@ class Solution: - def calPoints(self, ops: List[str]) -> int: + def calPoints(self, operations: List[str]) -> int: stk = [] - for op in ops: - if op == '+': + for op in operations: + if op == "+": stk.append(stk[-1] + stk[-2]) - elif op == 'D': + elif op == "D": stk.append(stk[-1] << 1) - elif op == 'C': + elif op == "C": stk.pop() else: stk.append(int(op)) diff --git a/solution/0600-0699/0682.Baseball Game/Solution.rs b/solution/0600-0699/0682.Baseball Game/Solution.rs index 66067e6a61c70..1450b3771566f 100644 --- a/solution/0600-0699/0682.Baseball Game/Solution.rs +++ b/solution/0600-0699/0682.Baseball Game/Solution.rs @@ -1,23 +1,23 @@ impl Solution { - pub fn cal_points(ops: Vec) -> i32 { - let mut stack = vec![]; - for op in ops { + pub fn cal_points(operations: Vec) -> i32 { + let mut stk = vec![]; + for op in operations { match op.as_str() { "+" => { - let n = stack.len(); - stack.push(stack[n - 1] + stack[n - 2]); + let n = stk.len(); + stk.push(stk[n - 1] + stk[n - 2]); } "D" => { - stack.push(stack.last().unwrap() * 2); + stk.push(stk.last().unwrap() * 2); } "C" => { - stack.pop(); + stk.pop(); } n => { - stack.push(n.parse::().unwrap()); + stk.push(n.parse::().unwrap()); } } } - stack.into_iter().sum() + stk.into_iter().sum() } } diff --git a/solution/0600-0699/0682.Baseball Game/Solution.ts b/solution/0600-0699/0682.Baseball Game/Solution.ts index 64d5e296bfb11..18654da69180a 100644 --- a/solution/0600-0699/0682.Baseball Game/Solution.ts +++ b/solution/0600-0699/0682.Baseball Game/Solution.ts @@ -1,16 +1,15 @@ -function calPoints(ops: string[]): number { - const stack = []; - for (const op of ops) { - const n = stack.length; +function calPoints(operations: string[]): number { + const stk: number[] = []; + for (const op of operations) { if (op === '+') { - stack.push(stack[n - 1] + stack[n - 2]); + stk.push(stk.at(-1)! + stk.at(-2)!); } else if (op === 'D') { - stack.push(stack[n - 1] * 2); + stk.push(stk.at(-1)! << 1); } else if (op === 'C') { - stack.pop(); + stk.pop(); } else { - stack.push(Number(op)); + stk.push(+op); } } - return stack.reduce((p, v) => p + v); + return stk.reduce((a, b) => a + b, 0); }