Skip to content

Commit c686235

Browse files
committed
feat: add solutions to lc problem: No.0946
No.0946.Validate Stack Sequences
1 parent 8fd1020 commit c686235

File tree

6 files changed

+172
-17
lines changed

6 files changed

+172
-17
lines changed

solution/0900-0999/0946.Validate Stack Sequences/README.md

+65-2
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,90 @@ push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
3636
<li><code>pushed</code>&nbsp;是&nbsp;<code>popped</code>&nbsp;的排列。</li>
3737
</ol>
3838

39-
4039
## 解法
4140

4241
<!-- 这里可写通用的实现逻辑 -->
4342

43+
遍历 pushed 序列,将每个数依次压入栈中,压入后检查这个数是不是 popped 序列中下一个要 pop 的值,如果是就把它 pop 出来。
44+
45+
最后检查所有数是否都 pop 出来了。
46+
4447
<!-- tabs:start -->
4548

4649
### **Python3**
4750

4851
<!-- 这里可写当前语言的特殊实现逻辑 -->
4952

5053
```python
51-
54+
class Solution:
55+
def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
56+
stk, j, n = [], 0, len(popped)
57+
for x in pushed:
58+
stk.append(x)
59+
while stk and j < n and stk[-1] == popped[j]:
60+
stk.pop()
61+
j += 1
62+
return j == n
5263
```
5364

5465
### **Java**
5566

5667
<!-- 这里可写当前语言的特殊实现逻辑 -->
5768

5869
```java
70+
class Solution {
71+
public boolean validateStackSequences(int[] pushed, int[] popped) {
72+
Deque<Integer> stk = new ArrayDeque<>();
73+
int j = 0, n = popped.length;
74+
for (int x : pushed) {
75+
stk.push(x);
76+
while (!stk.isEmpty() && j < n && stk.peek() == popped[j]) {
77+
stk.pop();
78+
++j;
79+
}
80+
}
81+
return j == n;
82+
}
83+
}
84+
```
85+
86+
### **C++**
87+
88+
```cpp
89+
class Solution {
90+
public:
91+
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
92+
int j = 0, n = popped.size();
93+
stack<int> stk;
94+
for (int x : pushed)
95+
{
96+
stk.push(x);
97+
while (!stk.empty() && j < n && stk.top() == popped[j])
98+
{
99+
stk.pop();
100+
++j;
101+
}
102+
}
103+
return j == n;
104+
}
105+
};
106+
```
59107
108+
### **Go**
109+
110+
```go
111+
func validateStackSequences(pushed []int, popped []int) bool {
112+
j, n := 0, len(popped)
113+
var stk []int
114+
for _, x := range pushed {
115+
stk = append(stk, x)
116+
for len(stk) > 0 && j < n && stk[len(stk)-1] == popped[j] {
117+
stk = stk[:len(stk)-1]
118+
j++
119+
}
120+
}
121+
return j == n
122+
}
60123
```
61124

62125
### **...**

solution/0900-0999/0946.Validate Stack Sequences/README_EN.md

+61-2
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,80 @@ push(5), pop() -&gt; 5, pop() -&gt; 3, pop() -&gt; 2, pop() -&gt; 1
4040
<li><code>pushed</code> and <code>popped</code> have distinct values.</li>
4141
</ul>
4242

43-
4443
## Solutions
4544

4645
<!-- tabs:start -->
4746

4847
### **Python3**
4948

5049
```python
51-
50+
class Solution:
51+
def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
52+
stk, j, n = [], 0, len(popped)
53+
for x in pushed:
54+
stk.append(x)
55+
while stk and j < n and stk[-1] == popped[j]:
56+
stk.pop()
57+
j += 1
58+
return j == n
5259
```
5360

5461
### **Java**
5562

5663
```java
64+
class Solution {
65+
public boolean validateStackSequences(int[] pushed, int[] popped) {
66+
Deque<Integer> stk = new ArrayDeque<>();
67+
int j = 0, n = popped.length;
68+
for (int x : pushed) {
69+
stk.push(x);
70+
while (!stk.isEmpty() && j < n && stk.peek() == popped[j]) {
71+
stk.pop();
72+
++j;
73+
}
74+
}
75+
return j == n;
76+
}
77+
}
78+
```
79+
80+
### **C++**
81+
82+
```cpp
83+
class Solution {
84+
public:
85+
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
86+
int j = 0, n = popped.size();
87+
stack<int> stk;
88+
for (int x : pushed)
89+
{
90+
stk.push(x);
91+
while (!stk.empty() && j < n && stk.top() == popped[j])
92+
{
93+
stk.pop();
94+
++j;
95+
}
96+
}
97+
return j == n;
98+
}
99+
};
100+
```
57101
102+
### **Go**
103+
104+
```go
105+
func validateStackSequences(pushed []int, popped []int) bool {
106+
j, n := 0, len(popped)
107+
var stk []int
108+
for _, x := range pushed {
109+
stk = append(stk, x)
110+
for len(stk) > 0 && j < n && stk[len(stk)-1] == popped[j] {
111+
stk = stk[:len(stk)-1]
112+
j++
113+
}
114+
}
115+
return j == n
116+
}
58117
```
59118

60119
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
4+
int j = 0, n = popped.size();
5+
stack<int> stk;
6+
for (int x : pushed)
7+
{
8+
stk.push(x);
9+
while (!stk.empty() && j < n && stk.top() == popped[j])
10+
{
11+
stk.pop();
12+
++j;
13+
}
14+
}
15+
return j == n;
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func validateStackSequences(pushed []int, popped []int) bool {
2+
j, n := 0, len(popped)
3+
var stk []int
4+
for _, x := range pushed {
5+
stk = append(stk, x)
6+
for len(stk) > 0 && j < n && stk[len(stk)-1] == popped[j] {
7+
stk = stk[:len(stk)-1]
8+
j++
9+
}
10+
}
11+
return j == n
12+
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1-
import java.util.*;
2-
31
class Solution {
42
public boolean validateStackSequences(int[] pushed, int[] popped) {
5-
Stack<Integer> stack = new Stack<>();
6-
int i = 0, k = 0;
7-
while (i < popped.length) {
8-
if (!stack.isEmpty() && popped[i] == stack.peek()) {
9-
stack.pop();
10-
i ++;
11-
} else {
12-
if (k < pushed.length) {
13-
stack.push(pushed[k ++]);
14-
} else return false;
3+
Deque<Integer> stk = new ArrayDeque<>();
4+
int j = 0, n = popped.length;
5+
for (int x : pushed) {
6+
stk.push(x);
7+
while (!stk.isEmpty() && j < n && stk.peek() == popped[j]) {
8+
stk.pop();
9+
++j;
1510
}
1611
}
17-
return stack.isEmpty();
12+
return j == n;
1813
}
1914
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
3+
stk, j, n = [], 0, len(popped)
4+
for x in pushed:
5+
stk.append(x)
6+
while stk and j < n and stk[-1] == popped[j]:
7+
stk.pop()
8+
j += 1
9+
return j == n

0 commit comments

Comments
 (0)