Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6dd9f80

Browse files
Merge pull request SharingSource#659 from SharingSource/ac_oier
✨feat: add 946
2 parents 45d0c17 + eaa546c commit 6dd9f80

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

‎Index/双指针.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
| [917. 仅仅反转字母](https://leetcode-cn.com/problems/reverse-only-letters/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reverse-only-letters/solution/gong-shui-san-xie-jian-dan-shuang-zhi-zh-xrpt/) | 简单 | 🤩🤩🤩🤩 |
4242
| [930. 和相同的二元子数组](https://leetcode-cn.com/problems/binary-subarrays-with-sum/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/binary-subarrays-with-sum/solution/gong-shui-san-xie-yi-ti-shuang-jie-qian-hfoc0/) | 中等 | 🤩🤩🤩 |
4343
| [942. 增减字符串匹配](https://leetcode.cn/problems/di-string-match/) | [LeetCode 题解链接](https://leetcode.cn/problems/di-string-match/solution/by-ac_oier-pvjk/) | 简单 | 🤩🤩🤩🤩 |
44+
| [946. 验证栈序列](https://leetcode.cn/problems/validate-stack-sequences/) | [LeetCode 题解链接](https://leetcode.cn/problems/validate-stack-sequences/solution/by-ac_oier-84qd/) | 中等 | 🤩🤩🤩🤩 |
4445
| [992. K 个不同整数的子数组](https://leetcode-cn.com/problems/subarrays-with-k-different-integers/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/subarrays-with-k-different-integers/solution/miao-dong-xi-lie-xiang-jie-shuang-zhi-zh-9k8w/) | 困难 | 🤩🤩🤩🤩 |
4546
| [1004. 最大连续1的个数 III](https://leetcode-cn.com/problems/max-consecutive-ones-iii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/max-consecutive-ones-iii/solution/san-chong-jie-fa-cong-dong-tai-gui-hua-d-gxks/) | 中等 | 🤩🤩🤩 |
4647
| [1021. 删除最外层的括号](https://leetcode.cn/problems/remove-outermost-parentheses/) | [LeetCode 题解链接](https://leetcode.cn/problems/remove-outermost-parentheses/solution/by-ac_oier-jmxi/) | 简单 | 🤩🤩🤩🤩 |

‎Index/栈.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
| [636. 函数的独占时间](https://leetcode.cn/problems/exclusive-time-of-functions/) | [LeetCode 题解链接](https://leetcode.cn/problems/exclusive-time-of-functions/solution/by-ac_oier-z3ed/) | 中等 | 🤩🤩🤩🤩🤩 |
1212
| [726. 原子的数量](https://leetcode-cn.com/problems/number-of-atoms/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-atoms/solution/gong-shui-san-xie-shi-yong-xiao-ji-qiao-l5ak4/) | 困难 | 🤩🤩🤩🤩 |
1313
| [735. 行星碰撞](https://leetcode.cn/problems/asteroid-collision/) | [LeetCode 题解链接](https://leetcode.cn/problems/asteroid-collision/solution/by-ac_oier-p4qh/) | 中等 | 🤩🤩🤩🤩🤩 |
14+
| [946. 验证栈序列](https://leetcode.cn/problems/validate-stack-sequences/) | [LeetCode 题解链接](https://leetcode.cn/problems/validate-stack-sequences/solution/by-ac_oier-84qd/) | 中等 | 🤩🤩🤩🤩 |
1415
| [1190. 反转每对括号间的子串](https://leetcode-cn.com/problems/reverse-substrings-between-each-pair-of-parentheses/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reverse-substrings-between-each-pair-of-parentheses/solution/gong-shui-san-xie-shi-yong-shuang-duan-d-r35q/) | 中等 | 🤩🤩🤩🤩🤩 |
1516
| [面试题 03.01. 三合一](https://leetcode-cn.com/problems/three-in-one-lcci/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/three-in-one-lcci/solution/yi-ti-shuang-jie-er-wei-shu-zu-yi-wei-sh-lih7/) | 简单 | 🤩🤩🤩 |
1617
| [面试题 02.05. 链表求和](https://leetcode-cn.com/problems/sum-lists-lcci/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sum-lists-lcci/solution/by-ac_oier-v1zb/) | 中等 | 🤩🤩🤩 |

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
| [919. 完全二叉树插入器](https://leetcode.cn/problems/complete-binary-tree-inserter/) | [LeetCode 题解链接](https://leetcode.cn/problems/complete-binary-tree-inserter/solution/by-ac_oier-t9dh/) | 中等 | 🤩🤩🤩🤩 |
120120
| [929. 独特的电子邮件地址](https://leetcode.cn/problems/unique-email-addresses/) | [LeetCode 题解链接](https://leetcode.cn/problems/unique-email-addresses/solution/by-ac_oier-d3zu/) | 简单 | 🤩🤩🤩 |
121121
| [944. 删列造序](https://leetcode.cn/problems/delete-columns-to-make-sorted/) | [LeetCode 题解链接](https://leetcode.cn/problems/delete-columns-to-make-sorted/solution/by-ac_oier-smoz/) | 简单 | 🤩🤩🤩 |
122+
| [946. 验证栈序列](https://leetcode.cn/problems/validate-stack-sequences/) | [LeetCode 题解链接](https://leetcode.cn/problems/validate-stack-sequences/solution/by-ac_oier-84qd/) | 中等 | 🤩🤩🤩🤩 |
122123
| [953. 验证外星语词典](https://leetcode.cn/problems/verifying-an-alien-dictionary/) | [LeetCode 题解链接](https://leetcode.cn/problems/verifying-an-alien-dictionary/solution/by-ac_oier-sxf1/) | 简单 | 🤩🤩🤩🤩 |
123124
| [961. 在长度 2N 的数组中找出重复 N 次的元素](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/) | [LeetCode 题解链接](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/solution/by-ac_oier-bslq/) | 简单 | 🤩🤩🤩🤩 |
124125
| [997. 找到小镇的法官](https://leetcode-cn.com/problems/find-the-town-judge/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-the-town-judge/solution/gong-shui-san-xie-jian-dan-chu-du-ru-du-5ms57/) | 简单 | 🤩🤩🤩🤩 |
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[946. 验证栈序列](https://leetcode.cn/problems/validate-stack-sequences/solution/by-ac_oier-84qd/)** ,难度为 **中等**
4+
5+
Tag : 「模拟」、「栈」、「双指针」
6+
7+
8+
9+
给定 `pushed``popped` 两个序列,每个序列中的 值都不重复,只有当它们可能是在最初空栈上进行的推入 `push` 和弹出 `pop` 操作序列的结果时,返回 `true`;否则,返回 `false`
10+
11+
示例 1:
12+
```
13+
输入:pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
14+
15+
输出:true
16+
17+
解释:我们可以按以下顺序执行:
18+
push(1), push(2), push(3), push(4), pop() -> 4,
19+
push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
20+
```
21+
示例 2:
22+
```
23+
输入:pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
24+
25+
输出:false
26+
27+
解释:1 不能在 2 之前弹出。
28+
```
29+
30+
提示:
31+
* 1ドル <= pushed.length <= 1000$
32+
* 0ドル <= pushed[i] <= 1000$
33+
* `pushed` 的所有元素 互不相同
34+
* $popped.length = pushed.length$
35+
* `popped``pushed` 的一个排列
36+
37+
---
38+
39+
### 栈运用模拟
40+
41+
根据题意,利用元素各不相同,我们使用一个栈来处理 `pushed` 数组,每次将 $pushed[i]$ 放入栈中,然后比较当前栈顶元素是否与待弹出元素相同(使用变量 `j` 来代指当前待弹出元素下标),若相等则弹栈并进行 `j` 自增,当所有的元素处理完后,栈为空说明栈序列合法。
42+
43+
Java 代码:
44+
```Java
45+
class Solution {
46+
public boolean validateStackSequences(int[] pushed, int[] popped) {
47+
Deque<Integer> d = new ArrayDeque<>();
48+
for (int i = 0, j = 0; i < pushed.length; i++) {
49+
d.addLast(pushed[i]);
50+
while (!d.isEmpty() && d.peekLast() == popped[j] && ++j >= 0) d.pollLast();
51+
}
52+
return d.isEmpty();
53+
}
54+
}
55+
```
56+
Typescript 代码:
57+
```Typescript
58+
function validateStackSequences(pushed: number[], popped: number[]): boolean {
59+
let n = pushed.length, he = 0, ta = 0
60+
const stk: number[] = new Array<number>(n).fill(0)
61+
for (let i = 0, j = 0; i < n; i++) {
62+
stk[ta++] = pushed[i]
63+
while (he < ta && stk[ta - 1] == popped[j] && ++j >= 0) ta--
64+
}
65+
return he == ta
66+
};
67+
```
68+
* 时间复杂度:$O(n)$
69+
* 空间复杂度:$O(n)$
70+
71+
---
72+
73+
### 双指针
74+
75+
我们也可以直接利用 `pushed` 充当栈,使用变量 `idx` 代指栈顶下标,变量 `j` 指向 `popped` 中待弹出的元素。
76+
77+
该做法好处无须额外空间,坏处是会修改入参数组。
78+
79+
Java 代码:
80+
```Java
81+
class Solution {
82+
public boolean validateStackSequences(int[] pushed, int[] popped) {
83+
int n = pushed.length, idx = 0;
84+
for (int i = 0, j = 0; i < n; i++) {
85+
pushed[idx++] = pushed[i];
86+
while (idx > 0 && pushed[idx - 1] == popped[j] && ++j >= 0) idx--;
87+
}
88+
return idx == 0;
89+
}
90+
}
91+
```
92+
TypeScript 代码:
93+
```TypeScript
94+
function validateStackSequences(pushed: number[], popped: number[]): boolean {
95+
let n = pushed.length, idx = 0
96+
for (let i = 0, j = 0; i < n; i++) {
97+
pushed[idx++] = pushed[i]
98+
while (idx > 0 && pushed[idx - 1] == popped[j] && ++j >= 0) idx--
99+
}
100+
return idx == 0
101+
};
102+
```
103+
* 时间复杂度:$O(n)$
104+
* 空间复杂度:$O(1)$
105+
106+
---
107+
108+
### 加餐
109+
110+
**加餐一道同类型题目 : [常规栈运用题](https://mp.weixin.qq.com/s?__biz=MzU4NDE3MTEyMA==&mid=2247493276&idx=1&sn=b4b1ff8e6b1794f4f2e4327399755b0c)🎉🎉**
111+
112+
---
113+
114+
### 最后
115+
116+
这是我们「刷穿 LeetCode」系列文章的第 `No.946` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
117+
118+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
119+
120+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
121+
122+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
123+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /