diff --git a/solution/0900-0999/0946.Validate Stack Sequences/README.md b/solution/0900-0999/0946.Validate Stack Sequences/README.md index c43f3658057a7..0643f0b745b4e 100644 --- a/solution/0900-0999/0946.Validate Stack Sequences/README.md +++ b/solution/0900-0999/0946.Validate Stack Sequences/README.md @@ -60,11 +60,9 @@ push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1 ### 方法一:栈模拟 -遍历 `pushed` 序列,将每个数 `v` 依次压入栈中,压入后检查这个数是不是 `popped` 序列中下一个要弹出的值,如果是就循环把栈顶元素弹出。 +我们遍历 $\textit{pushed}$ 数组,对于当前遍历到的元素 $x$,我们将其压入栈 $\textit{stk}$ 中,然后判断栈顶元素是否和 $\textit{popped}$ 数组中下一个要弹出的元素相等,如果相等,我们就将栈顶元素弹出并将 $\textit{popped}$ 数组中下一个要弹出的元素的索引 $i$ 加一。最后,如果要弹出的元素都能按照 $\textit{popped}$ 数组的顺序弹出,返回 $\textit{true}$,否则返回 $\textit{false}$。 -遍历结束,如果 `popped` 序列已经到末尾,说明是一个合法的序列,否则不是。 - -时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是 `pushed` 序列的长度。 +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 $\textit{pushed}$ 数组的长度。 @@ -73,13 +71,14 @@ push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1 ```python class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: - j, stk = 0, [] - for v in pushed: - stk.append(v) - while stk and stk[-1] == popped[j]: + stk = [] + i = 0 + for x in pushed: + stk.append(x) + while stk and stk[-1] == popped[i]: stk.pop() - j += 1 - return j == len(pushed) + i += 1 + return i == len(popped) ``` #### Java @@ -88,15 +87,15 @@ class Solution: class Solution { public boolean validateStackSequences(int[] pushed, int[] popped) { Deque stk = new ArrayDeque<>(); - int j = 0; - for (int v : pushed) { - stk.push(v); - while (!stk.isEmpty() && stk.peek() == popped[j]) { + int i = 0; + for (int x : pushed) { + stk.push(x); + while (!stk.isEmpty() && stk.peek() == popped[i]) { stk.pop(); - ++j; + ++i; } } - return j == pushed.length; + return i == popped.length; } } ``` @@ -108,15 +107,15 @@ class Solution { public: bool validateStackSequences(vector& pushed, vector& popped) { stack stk; - int j = 0; - for (int v : pushed) { - stk.push(v); - while (!stk.empty() && stk.top() == popped[j]) { + int i = 0; + for (int x : pushed) { + stk.push(x); + while (stk.size() && stk.top() == popped[i]) { stk.pop(); - ++j; + ++i; } } - return j == pushed.size(); + return i == popped.size(); } }; ``` @@ -126,15 +125,15 @@ public: ```go func validateStackSequences(pushed []int, popped []int) bool { stk := []int{} - j := 0 - for _, v := range pushed { - stk = append(stk, v) - for len(stk) > 0 && stk[len(stk)-1] == popped[j] { + i := 0 + for _, x := range pushed { + stk = append(stk, x) + for len(stk) > 0 && stk[len(stk)-1] == popped[i] { stk = stk[:len(stk)-1] - j++ + i++ } } - return j == len(pushed) + return i == len(popped) } ``` @@ -142,16 +141,16 @@ func validateStackSequences(pushed []int, popped []int) bool { ```ts function validateStackSequences(pushed: number[], popped: number[]): boolean { - const stk = []; - let j = 0; - for (const v of pushed) { - stk.push(v); - while (stk.length && stk[stk.length - 1] == popped[j]) { + const stk: number[] = []; + let i = 0; + for (const x of pushed) { + stk.push(x); + while (stk.length && stk.at(-1)! === popped[i]) { stk.pop(); - ++j; + i++; } } - return j == pushed.length; + return i === popped.length; } ``` @@ -160,16 +159,16 @@ function validateStackSequences(pushed: number[], popped: number[]): boolean { ```rust impl Solution { pub fn validate_stack_sequences(pushed: Vec, popped: Vec) -> bool { - let mut stack = Vec::new(); + let mut stk: Vec = Vec::new(); let mut i = 0; - for &num in pushed.iter() { - stack.push(num); - while !stack.is_empty() && *stack.last().unwrap() == popped[i] { - stack.pop(); + for &x in &pushed { + stk.push(x); + while !stk.is_empty() && *stk.last().unwrap() == popped[i] { + stk.pop(); i += 1; } } - stack.len() == 0 + i == popped.len() } } ``` @@ -183,16 +182,16 @@ impl Solution { * @return {boolean} */ var validateStackSequences = function (pushed, popped) { - let stk = []; - let j = 0; - for (const v of pushed) { - stk.push(v); - while (stk.length && stk[stk.length - 1] == popped[j]) { + const stk = []; + let i = 0; + for (const x of pushed) { + stk.push(x); + while (stk.length && stk.at(-1) === popped[i]) { stk.pop(); - ++j; + i++; } } - return j == pushed.length; + return i === popped.length; }; ``` @@ -202,16 +201,17 @@ var validateStackSequences = function (pushed, popped) { public class Solution { public bool ValidateStackSequences(int[] pushed, int[] popped) { Stack stk = new Stack(); - int j = 0; - foreach (int x in pushed) - { + int i = 0; + + foreach (int x in pushed) { stk.Push(x); - while (stk.Count != 0 && stk.Peek() == popped[j]) { + while (stk.Count > 0 && stk.Peek() == popped[i]) { stk.Pop(); - ++j; + i++; } } - return stk.Count == 0; + + return i == popped.Length; } } ``` diff --git a/solution/0900-0999/0946.Validate Stack Sequences/README_EN.md b/solution/0900-0999/0946.Validate Stack Sequences/README_EN.md index da4100975da3b..e837d2f87d45f 100644 --- a/solution/0900-0999/0946.Validate Stack Sequences/README_EN.md +++ b/solution/0900-0999/0946.Validate Stack Sequences/README_EN.md @@ -58,7 +58,11 @@ pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1 -### Solution 1 +### Solution 1: Stack Simulation + +We iterate through the $\textit{pushed}$ array. For the current element $x$ being iterated, we push it into the stack $\textit{stk}$. Then, we check if the top element of the stack is equal to the next element to be popped in the $\textit{popped}$ array. If they are equal, we pop the top element from the stack and increment the index $i$ of the next element to be popped in the $\textit{popped}$ array. Finally, if all elements can be popped in the order specified by the $\textit{popped}$ array, return $\textit{true}$; otherwise, return $\textit{false}$. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the $\textit{pushed}$ array. @@ -67,13 +71,14 @@ pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1 ```python class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: - j, stk = 0, [] - for v in pushed: - stk.append(v) - while stk and stk[-1] == popped[j]: + stk = [] + i = 0 + for x in pushed: + stk.append(x) + while stk and stk[-1] == popped[i]: stk.pop() - j += 1 - return j == len(pushed) + i += 1 + return i == len(popped) ``` #### Java @@ -82,15 +87,15 @@ class Solution: class Solution { public boolean validateStackSequences(int[] pushed, int[] popped) { Deque stk = new ArrayDeque<>(); - int j = 0; - for (int v : pushed) { - stk.push(v); - while (!stk.isEmpty() && stk.peek() == popped[j]) { + int i = 0; + for (int x : pushed) { + stk.push(x); + while (!stk.isEmpty() && stk.peek() == popped[i]) { stk.pop(); - ++j; + ++i; } } - return j == pushed.length; + return i == popped.length; } } ``` @@ -102,15 +107,15 @@ class Solution { public: bool validateStackSequences(vector& pushed, vector& popped) { stack stk; - int j = 0; - for (int v : pushed) { - stk.push(v); - while (!stk.empty() && stk.top() == popped[j]) { + int i = 0; + for (int x : pushed) { + stk.push(x); + while (stk.size() && stk.top() == popped[i]) { stk.pop(); - ++j; + ++i; } } - return j == pushed.size(); + return i == popped.size(); } }; ``` @@ -120,15 +125,15 @@ public: ```go func validateStackSequences(pushed []int, popped []int) bool { stk := []int{} - j := 0 - for _, v := range pushed { - stk = append(stk, v) - for len(stk) > 0 && stk[len(stk)-1] == popped[j] { + i := 0 + for _, x := range pushed { + stk = append(stk, x) + for len(stk) > 0 && stk[len(stk)-1] == popped[i] { stk = stk[:len(stk)-1] - j++ + i++ } } - return j == len(pushed) + return i == len(popped) } ``` @@ -136,16 +141,16 @@ func validateStackSequences(pushed []int, popped []int) bool { ```ts function validateStackSequences(pushed: number[], popped: number[]): boolean { - const stk = []; - let j = 0; - for (const v of pushed) { - stk.push(v); - while (stk.length && stk[stk.length - 1] == popped[j]) { + const stk: number[] = []; + let i = 0; + for (const x of pushed) { + stk.push(x); + while (stk.length && stk.at(-1)! === popped[i]) { stk.pop(); - ++j; + i++; } } - return j == pushed.length; + return i === popped.length; } ``` @@ -154,16 +159,16 @@ function validateStackSequences(pushed: number[], popped: number[]): boolean { ```rust impl Solution { pub fn validate_stack_sequences(pushed: Vec, popped: Vec) -> bool { - let mut stack = Vec::new(); + let mut stk: Vec = Vec::new(); let mut i = 0; - for &num in pushed.iter() { - stack.push(num); - while !stack.is_empty() && *stack.last().unwrap() == popped[i] { - stack.pop(); + for &x in &pushed { + stk.push(x); + while !stk.is_empty() && *stk.last().unwrap() == popped[i] { + stk.pop(); i += 1; } } - stack.len() == 0 + i == popped.len() } } ``` @@ -177,16 +182,16 @@ impl Solution { * @return {boolean} */ var validateStackSequences = function (pushed, popped) { - let stk = []; - let j = 0; - for (const v of pushed) { - stk.push(v); - while (stk.length && stk[stk.length - 1] == popped[j]) { + const stk = []; + let i = 0; + for (const x of pushed) { + stk.push(x); + while (stk.length && stk.at(-1) === popped[i]) { stk.pop(); - ++j; + i++; } } - return j == pushed.length; + return i === popped.length; }; ``` @@ -196,16 +201,17 @@ var validateStackSequences = function (pushed, popped) { public class Solution { public bool ValidateStackSequences(int[] pushed, int[] popped) { Stack stk = new Stack(); - int j = 0; - foreach (int x in pushed) - { + int i = 0; + + foreach (int x in pushed) { stk.Push(x); - while (stk.Count != 0 && stk.Peek() == popped[j]) { + while (stk.Count > 0 && stk.Peek() == popped[i]) { stk.Pop(); - ++j; + i++; } } - return stk.Count == 0; + + return i == popped.Length; } } ``` diff --git a/solution/0900-0999/0946.Validate Stack Sequences/Solution.cpp b/solution/0900-0999/0946.Validate Stack Sequences/Solution.cpp index 23c5e29fb158a..47e56d9526b27 100644 --- a/solution/0900-0999/0946.Validate Stack Sequences/Solution.cpp +++ b/solution/0900-0999/0946.Validate Stack Sequences/Solution.cpp @@ -2,14 +2,14 @@ class Solution { public: bool validateStackSequences(vector& pushed, vector& popped) { stack stk; - int j = 0; - for (int v : pushed) { - stk.push(v); - while (!stk.empty() && stk.top() == popped[j]) { + int i = 0; + for (int x : pushed) { + stk.push(x); + while (stk.size() && stk.top() == popped[i]) { stk.pop(); - ++j; + ++i; } } - return j == pushed.size(); + return i == popped.size(); } }; \ No newline at end of file diff --git a/solution/0900-0999/0946.Validate Stack Sequences/Solution.cs b/solution/0900-0999/0946.Validate Stack Sequences/Solution.cs index eb939e6252c72..d2ab729de5b73 100644 --- a/solution/0900-0999/0946.Validate Stack Sequences/Solution.cs +++ b/solution/0900-0999/0946.Validate Stack Sequences/Solution.cs @@ -1,15 +1,16 @@ public class Solution { public bool ValidateStackSequences(int[] pushed, int[] popped) { Stack stk = new Stack(); - int j = 0; - foreach (int x in pushed) - { + int i = 0; + + foreach (int x in pushed) { stk.Push(x); - while (stk.Count != 0 && stk.Peek() == popped[j]) { + while (stk.Count > 0 && stk.Peek() == popped[i]) { stk.Pop(); - ++j; + i++; } } - return stk.Count == 0; + + return i == popped.Length; } -} +} \ No newline at end of file diff --git a/solution/0900-0999/0946.Validate Stack Sequences/Solution.go b/solution/0900-0999/0946.Validate Stack Sequences/Solution.go index 7216a0ab5e82b..80099b6b98d21 100644 --- a/solution/0900-0999/0946.Validate Stack Sequences/Solution.go +++ b/solution/0900-0999/0946.Validate Stack Sequences/Solution.go @@ -1,12 +1,12 @@ func validateStackSequences(pushed []int, popped []int) bool { stk := []int{} - j := 0 - for _, v := range pushed { - stk = append(stk, v) - for len(stk) > 0 && stk[len(stk)-1] == popped[j] { + i := 0 + for _, x := range pushed { + stk = append(stk, x) + for len(stk) > 0 && stk[len(stk)-1] == popped[i] { stk = stk[:len(stk)-1] - j++ + i++ } } - return j == len(pushed) + return i == len(popped) } \ No newline at end of file diff --git a/solution/0900-0999/0946.Validate Stack Sequences/Solution.java b/solution/0900-0999/0946.Validate Stack Sequences/Solution.java index aec88bfb9310d..3a15272260eeb 100644 --- a/solution/0900-0999/0946.Validate Stack Sequences/Solution.java +++ b/solution/0900-0999/0946.Validate Stack Sequences/Solution.java @@ -1,14 +1,14 @@ class Solution { public boolean validateStackSequences(int[] pushed, int[] popped) { Deque stk = new ArrayDeque<>(); - int j = 0; - for (int v : pushed) { - stk.push(v); - while (!stk.isEmpty() && stk.peek() == popped[j]) { + int i = 0; + for (int x : pushed) { + stk.push(x); + while (!stk.isEmpty() && stk.peek() == popped[i]) { stk.pop(); - ++j; + ++i; } } - return j == pushed.length; + return i == popped.length; } } \ No newline at end of file diff --git a/solution/0900-0999/0946.Validate Stack Sequences/Solution.js b/solution/0900-0999/0946.Validate Stack Sequences/Solution.js index 64bd4910986ed..da12ecdf91a35 100644 --- a/solution/0900-0999/0946.Validate Stack Sequences/Solution.js +++ b/solution/0900-0999/0946.Validate Stack Sequences/Solution.js @@ -4,14 +4,14 @@ * @return {boolean} */ var validateStackSequences = function (pushed, popped) { - let stk = []; - let j = 0; - for (const v of pushed) { - stk.push(v); - while (stk.length && stk[stk.length - 1] == popped[j]) { + const stk = []; + let i = 0; + for (const x of pushed) { + stk.push(x); + while (stk.length && stk.at(-1) === popped[i]) { stk.pop(); - ++j; + i++; } } - return j == pushed.length; + return i === popped.length; }; diff --git a/solution/0900-0999/0946.Validate Stack Sequences/Solution.py b/solution/0900-0999/0946.Validate Stack Sequences/Solution.py index 25e1d6b0b7710..d06286cbc523e 100644 --- a/solution/0900-0999/0946.Validate Stack Sequences/Solution.py +++ b/solution/0900-0999/0946.Validate Stack Sequences/Solution.py @@ -1,9 +1,10 @@ class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: - j, stk = 0, [] - for v in pushed: - stk.append(v) - while stk and stk[-1] == popped[j]: + stk = [] + i = 0 + for x in pushed: + stk.append(x) + while stk and stk[-1] == popped[i]: stk.pop() - j += 1 - return j == len(pushed) + i += 1 + return i == len(popped) diff --git a/solution/0900-0999/0946.Validate Stack Sequences/Solution.rs b/solution/0900-0999/0946.Validate Stack Sequences/Solution.rs index df75d0659a35f..bc67c35b812d6 100644 --- a/solution/0900-0999/0946.Validate Stack Sequences/Solution.rs +++ b/solution/0900-0999/0946.Validate Stack Sequences/Solution.rs @@ -1,14 +1,14 @@ impl Solution { pub fn validate_stack_sequences(pushed: Vec, popped: Vec) -> bool { - let mut stack = Vec::new(); + let mut stk: Vec = Vec::new(); let mut i = 0; - for &num in pushed.iter() { - stack.push(num); - while !stack.is_empty() && *stack.last().unwrap() == popped[i] { - stack.pop(); + for &x in &pushed { + stk.push(x); + while !stk.is_empty() && *stk.last().unwrap() == popped[i] { + stk.pop(); i += 1; } } - stack.len() == 0 + i == popped.len() } } diff --git a/solution/0900-0999/0946.Validate Stack Sequences/Solution.ts b/solution/0900-0999/0946.Validate Stack Sequences/Solution.ts index 4eac7307db9d2..d0f4d1a9d53b9 100644 --- a/solution/0900-0999/0946.Validate Stack Sequences/Solution.ts +++ b/solution/0900-0999/0946.Validate Stack Sequences/Solution.ts @@ -1,12 +1,12 @@ function validateStackSequences(pushed: number[], popped: number[]): boolean { - const stk = []; - let j = 0; - for (const v of pushed) { - stk.push(v); - while (stk.length && stk[stk.length - 1] == popped[j]) { + const stk: number[] = []; + let i = 0; + for (const x of pushed) { + stk.push(x); + while (stk.length && stk.at(-1)! === popped[i]) { stk.pop(); - ++j; + i++; } } - return j == pushed.length; + return i === popped.length; }