Skip to content

Commit 709eb23

Browse files
committed
feat: add solutions to lc/lcof problems
lc No.0946 & lcof No.31.Validate Stack Sequences
1 parent 4a05f6b commit 709eb23

File tree

18 files changed

+434
-183
lines changed

18 files changed

+434
-183
lines changed

lcof/面试题31. 栈的压入、弹出序列/README.md

Lines changed: 92 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
4040

4141
<!-- 这里可写通用的实现逻辑 -->
4242

43-
借助一个辅助栈实现。
43+
**方法一:栈模拟**
44+
45+
遍历 `pushed` 序列,将每个数 `v` 依次压入栈中,压入后检查这个数是不是 `popped` 序列中下一个要弹出的值,如果是就循环把栈顶元素弹出。
46+
47+
遍历结束,如果 `popped` 序列已经到末尾,说明是一个合法的序列,否则不是。
48+
49+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是 `pushed` 序列的长度。
4450

4551
<!-- tabs:start -->
4652

@@ -51,14 +57,13 @@ push(5), pop() -&gt; 5, pop() -&gt; 3, pop() -&gt; 2, pop() -&gt; 1
5157
```python
5258
class Solution:
5359
def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
54-
s = []
55-
q = 0
56-
for num in pushed:
57-
s.append(num)
58-
while s and s[-1] == popped[q]:
59-
s.pop()
60-
q += 1
61-
return not s
60+
j, stk = 0, []
61+
for v in pushed:
62+
stk.append(v)
63+
while stk and stk[-1] == popped[j]:
64+
stk.pop()
65+
j += 1
66+
return j == len(pushed)
6267
```
6368

6469
### **Java**
@@ -68,76 +73,91 @@ class Solution:
6873
```java
6974
class Solution {
7075
public boolean validateStackSequences(int[] pushed, int[] popped) {
71-
Deque<Integer> s = new ArrayDeque<>();
72-
int q = 0;
73-
for (int num : pushed) {
74-
s.push(num);
75-
while (!s.isEmpty() && s.peek() == popped[q]) {
76-
s.pop();
77-
++q;
76+
Deque<Integer> stk = new ArrayDeque<>();
77+
int j = 0;
78+
for (int v : pushed) {
79+
stk.push(v);
80+
while (!stk.isEmpty() && stk.peek() == popped[j]) {
81+
stk.pop();
82+
++j;
7883
}
7984
}
80-
return s.isEmpty();
85+
return j == pushed.length;
8186
}
8287
}
8388
```
8489

85-
### **JavaScript**
86-
87-
```js
88-
/**
89-
* @param {number[]} pushed
90-
* @param {number[]} popped
91-
* @return {boolean}
92-
*/
93-
var validateStackSequences = function (pushed, popped) {
94-
let s = [];
95-
let q = 0;
96-
for (let num of pushed) {
97-
s.push(num);
98-
while (s.length > 0 && s[s.length - 1] == popped[q]) {
99-
++q;
100-
s.pop();
101-
}
102-
}
103-
return s.length == 0;
104-
};
105-
```
106-
10790
### **C++**
10891

10992
```cpp
11093
class Solution {
11194
public:
11295
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
113-
stack<int> s;
114-
int i = 0;
115-
for (int x : pushed) {
116-
s.push(x);
117-
while (!s.empty() && s.top() == popped[i]) {
118-
s.pop();
119-
++i;
96+
stack<int> stk;
97+
int j = 0;
98+
for (int v : pushed) {
99+
stk.push(v);
100+
while (!stk.empty() && stk.top() == popped[j]) {
101+
stk.pop();
102+
++j;
120103
}
121104
}
122-
return s.empty();
105+
return j == pushed.size();
123106
}
124107
};
125108
```
126109
110+
### **Go**
111+
112+
```go
113+
func validateStackSequences(pushed []int, popped []int) bool {
114+
stk := []int{}
115+
j := 0
116+
for _, v := range pushed {
117+
stk = append(stk, v)
118+
for len(stk) > 0 && stk[len(stk)-1] == popped[j] {
119+
stk = stk[:len(stk)-1]
120+
j++
121+
}
122+
}
123+
return j == len(pushed)
124+
}
125+
```
126+
127127
### **TypeScript**
128128

129129
```ts
130130
function validateStackSequences(pushed: number[], popped: number[]): boolean {
131-
const stack = [];
132-
let i = 0;
133-
for (const num of pushed) {
134-
stack.push(num);
135-
while (stack.length !== 0 && stack[stack.length - 1] === popped[i]) {
136-
stack.pop();
137-
i++;
131+
const stk = [];
132+
let j = 0;
133+
for (const v of pushed) {
134+
stk.push(v);
135+
while (stk.length && stk[stk.length - 1] == popped[j]) {
136+
stk.pop();
137+
++j;
138+
}
139+
}
140+
return j == pushed.length;
141+
}
142+
```
143+
144+
### **C#**
145+
146+
```cs
147+
public class Solution {
148+
public bool ValidateStackSequences(int[] pushed, int[] popped) {
149+
Stack<int> stk = new Stack<int>();
150+
int j = 0;
151+
foreach (int x in pushed)
152+
{
153+
stk.Push(x);
154+
while (stk.Count != 0 && stk.Peek() == popped[j]) {
155+
stk.Pop();
156+
++j;
157+
}
138158
}
159+
return stk.Count == 0;
139160
}
140-
return stack.length === 0;
141161
}
142162
```
143163

@@ -160,24 +180,26 @@ impl Solution {
160180
}
161181
```
162182

163-
### **C#**
183+
### **JavaScript**
164184

165-
```cs
166-
public class Solution {
167-
public bool ValidateStackSequences(int[] pushed, int[] popped) {
168-
Stack<int> ans = new Stack<int>();
169-
int q = 0;
170-
foreach (int x in pushed)
171-
{
172-
ans.Push(pushed[x]);
173-
while (ans.Count != 0 && ans.Peek() == popped[q]) {
174-
ans.Pop();
175-
q += 1;
176-
}
185+
```js
186+
/**
187+
* @param {number[]} pushed
188+
* @param {number[]} popped
189+
* @return {boolean}
190+
*/
191+
var validateStackSequences = function (pushed, popped) {
192+
let stk = [];
193+
let j = 0;
194+
for (const v of pushed) {
195+
stk.push(v);
196+
while (stk.length && stk[stk.length - 1] == popped[j]) {
197+
stk.pop();
198+
++j;
177199
}
178-
return ans.Count == 0;
179200
}
180-
}
201+
return j == pushed.length;
202+
};
181203
```
182204

183205
### **...**
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
class Solution {
22
public:
33
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
4-
stack<int> s;
5-
int i = 0;
6-
for (int x : pushed) {
7-
s.push(x);
8-
while (!s.empty() && s.top() == popped[i]) {
9-
s.pop();
10-
++i;
4+
stack<int> stk;
5+
int j = 0;
6+
for (int v : pushed) {
7+
stk.push(v);
8+
while (!stk.empty() && stk.top() == popped[j]) {
9+
stk.pop();
10+
++j;
1111
}
1212
}
13-
return s.empty();
13+
return j == pushed.size();
1414
}
15-
};
15+
};
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
public class Solution {
22
public bool ValidateStackSequences(int[] pushed, int[] popped) {
3-
Stack<int> ans = new Stack<int>();
4-
int q = 0;
3+
Stack<int> stk = new Stack<int>();
4+
int j = 0;
55
foreach (int x in pushed)
66
{
7-
ans.Push(pushed[x]);
8-
while (ans.Count != 0 && ans.Peek() == popped[q]) {
9-
ans.Pop();
10-
q += 1;
7+
stk.Push(x);
8+
while (stk.Count != 0 && stk.Peek() == popped[j]) {
9+
stk.Pop();
10+
++j;
1111
}
1212
}
13-
return ans.Count == 0;
13+
return stk.Count == 0;
1414
}
1515
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func validateStackSequences(pushed []int, popped []int) bool {
2+
stk := []int{}
3+
j := 0
4+
for _, v := range pushed {
5+
stk = append(stk, v)
6+
for len(stk) > 0 && stk[len(stk)-1] == popped[j] {
7+
stk = stk[:len(stk)-1]
8+
j++
9+
}
10+
}
11+
return j == len(pushed)
12+
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
class Solution {
22
public boolean validateStackSequences(int[] pushed, int[] popped) {
3-
Deque<Integer> s = new ArrayDeque<>();
4-
int q = 0;
5-
for (int num : pushed) {
6-
s.push(num);
7-
while (!s.isEmpty() && s.peek() == popped[q]) {
8-
s.pop();
9-
++q;
3+
Deque<Integer> stk = new ArrayDeque<>();
4+
int j = 0;
5+
for (int v : pushed) {
6+
stk.push(v);
7+
while (!stk.isEmpty() && stk.peek() == popped[j]) {
8+
stk.pop();
9+
++j;
1010
}
1111
}
12-
return s.isEmpty();
12+
return j == pushed.length;
1313
}
1414
}

lcof/面试题31. 栈的压入、弹出序列/Solution.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
* @return {boolean}
55
*/
66
var validateStackSequences = function (pushed, popped) {
7-
let s = [];
8-
let q = 0;
9-
for (let num of pushed) {
10-
s.push(num);
11-
while (s.length > 0 && s[s.length - 1] == popped[q]) {
12-
++q;
13-
s.pop();
7+
let stk = [];
8+
let j = 0;
9+
for (const v of pushed) {
10+
stk.push(v);
11+
while (stk.length && stk[stk.length - 1] == popped[j]) {
12+
stk.pop();
13+
++j;
1414
}
1515
}
16-
return s.length == 0;
16+
return j == pushed.length;
1717
};
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
class Solution:
22
def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
3-
s = []
4-
q = 0
5-
for num in pushed:
6-
s.append(num)
7-
while s and s[-1] == popped[q]:
8-
s.pop()
9-
q += 1
10-
return not s
3+
j, stk = 0, []
4+
for v in pushed:
5+
stk.append(v)
6+
while stk and stk[-1] == popped[j]:
7+
stk.pop()
8+
j += 1
9+
return j == len(pushed)
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
function validateStackSequences(pushed: number[], popped: number[]): boolean {
2-
const stack = [];
3-
let i = 0;
4-
for (const num of pushed) {
5-
stack.push(num);
6-
while (stack.length !== 0 && stack[stack.length - 1] === popped[i]) {
7-
stack.pop();
8-
i++;
2+
const stk = [];
3+
let j = 0;
4+
for (const v of pushed) {
5+
stk.push(v);
6+
while (stk.length && stk[stk.length - 1] == popped[j]) {
7+
stk.pop();
8+
++j;
99
}
1010
}
11-
return stack.length === 0;
11+
return j == pushed.length;
1212
}

0 commit comments

Comments
 (0)