Skip to content

feat: add solutions to lc problem: No.0946 #3294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 54 additions & 54 deletions solution/0900-0999/0946.Validate Stack Sequences/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}$ 数组的长度。

<!-- tabs:start -->

Expand All @@ -73,13 +71,14 @@ push(5), pop() -&gt; 5, pop() -&gt; 3, pop() -&gt; 2, pop() -&gt; 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
Expand All @@ -88,15 +87,15 @@ class Solution:
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
Deque<Integer> 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;
}
}
```
Expand All @@ -108,15 +107,15 @@ class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
stack<int> 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();
}
};
```
Expand All @@ -126,32 +125,32 @@ 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)
}
```

#### TypeScript

```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;
}
```

Expand All @@ -160,16 +159,16 @@ function validateStackSequences(pushed: number[], popped: number[]): boolean {
```rust
impl Solution {
pub fn validate_stack_sequences(pushed: Vec<i32>, popped: Vec<i32>) -> bool {
let mut stack = Vec::new();
let mut stk: Vec<i32> = 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()
}
}
```
Expand All @@ -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;
};
```

Expand All @@ -202,16 +201,17 @@ var validateStackSequences = function (pushed, popped) {
public class Solution {
public bool ValidateStackSequences(int[] pushed, int[] popped) {
Stack<int> stk = new Stack<int>();
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;
}
}
```
Expand Down
108 changes: 57 additions & 51 deletions solution/0900-0999/0946.Validate Stack Sequences/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ pop() -&gt; 5, pop() -&gt; 3, pop() -&gt; 2, pop() -&gt; 1

<!-- solution:start -->

### 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.

<!-- tabs:start -->

Expand All @@ -67,13 +71,14 @@ pop() -&gt; 5, pop() -&gt; 3, pop() -&gt; 2, pop() -&gt; 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
Expand All @@ -82,15 +87,15 @@ class Solution:
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
Deque<Integer> 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;
}
}
```
Expand All @@ -102,15 +107,15 @@ class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
stack<int> 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();
}
};
```
Expand All @@ -120,32 +125,32 @@ 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)
}
```

#### TypeScript

```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;
}
```

Expand All @@ -154,16 +159,16 @@ function validateStackSequences(pushed: number[], popped: number[]): boolean {
```rust
impl Solution {
pub fn validate_stack_sequences(pushed: Vec<i32>, popped: Vec<i32>) -> bool {
let mut stack = Vec::new();
let mut stk: Vec<i32> = 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()
}
}
```
Expand All @@ -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;
};
```

Expand All @@ -196,16 +201,17 @@ var validateStackSequences = function (pushed, popped) {
public class Solution {
public bool ValidateStackSequences(int[] pushed, int[] popped) {
Stack<int> stk = new Stack<int>();
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;
}
}
```
Expand Down
Loading
Loading