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 fa5fb92

Browse files
✨feat: Add 385
1 parent 9e12f7c commit fa5fb92

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

‎Index/栈.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
| [155. 最小栈](https://leetcode-cn.com/problems/min-stack/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/min-stack/solution/tu-li-zhan-shi-shuang-zhan-shi-xian-zui-fcwj5/) | 简单 | 🤩🤩🤩🤩🤩 |
77
| [232. 用栈实现队列](https://leetcode-cn.com/problems/implement-queue-using-stacks/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/implement-queue-using-stacks/solution/sha-shi-jun-tan-fu-za-du-ya-wo-de-suan-f-gb6d/) | 简单 | 🤩🤩🤩🤩🤩 |
88
| [341. 扁平化嵌套列表迭代器](https://leetcode-cn.com/problems/flatten-nested-list-iterator/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/flatten-nested-list-iterator/solution/yi-ti-shuang-jie-dfsdui-lie-di-gui-zhan-kvwhy/) | 中等 | 🤩🤩🤩 |
9+
| [385. 迷你语法分析器](https://leetcode-cn.com/problems/mini-parser/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/mini-parser/solution/by-ac_oier-zuy6/) | 中等 | 🤩🤩🤩🤩🤩 |
910
| [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/) | 困难 | 🤩🤩🤩🤩 |
1011
| [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/) | 中等 | 🤩🤩🤩🤩🤩 |
1112
| [面试题 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/) | 简单 | 🤩🤩🤩 |

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
| [345. 反转字符串中的元音字母](https://leetcode-cn.com/problems/reverse-vowels-of-a-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reverse-vowels-of-a-string/solution/gong-shui-san-xie-note-bie-pian-shuang-z-c8ii/) | 简单 | 🤩🤩🤩 |
4141
| [382. 链表随机节点](https://leetcode-cn.com/problems/linked-list-random-node/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/linked-list-random-node/solution/gong-shui-san-xie-xu-shui-chi-chou-yang-1lp9d/) | 中等 | 🤩🤩🤩🤩🤩 |
4242
| [383. 赎金信](https://leetcode-cn.com/problems/ransom-note/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/ransom-note/solution/gong-shui-san-xie-jian-dan-ji-shu-mo-ni-udpzn/) | 简单 | 🤩🤩🤩🤩🤩 |
43+
| [385. 迷你语法分析器](https://leetcode-cn.com/problems/mini-parser/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/mini-parser/solution/by-ac_oier-zuy6/) | 中等 | 🤩🤩🤩🤩🤩 |
4344
| [393. UTF-8 编码验证](https://leetcode-cn.com/problems/utf-8-validation/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/utf-8-validation/solution/by-ac_oier-zvoy/) | 中等 | 🤩🤩🤩🤩🤩 |
4445
| [400. 第 N 位数字](https://leetcode-cn.com/problems/nth-digit/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/nth-digit/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-w5wl/) | 中等 | 🤩🤩🤩🤩 |
4546
| [405. 数字转换为十六进制数](https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/solution/gong-shui-san-xie-yi-ti-shuang-jie-jin-z-d93o/) | 简单 | 🤩🤩🤩🤩 |
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[385. 迷你语法分析器](https://leetcode-cn.com/problems/mini-parser/solution/by-ac_oier-zuy6/)** ,难度为 **中等**
4+
5+
Tag : 「栈」、「模拟」
6+
7+
8+
9+
给定一个字符串 `s` 表示一个整数嵌套列表,实现一个解析它的语法分析器并返回解析的结果 `NestedInteger`
10+
11+
列表中的每个元素只可能是整数或整数嵌套列表
12+
13+
示例 1:
14+
```
15+
输入:s = "324",
16+
17+
输出:324
18+
19+
解释:你应该返回一个 NestedInteger 对象,其中只包含整数值 324。
20+
```
21+
示例 2:
22+
```
23+
输入:s = "[123,[456,[789]]]",
24+
25+
输出:[123,[456,[789]]]
26+
27+
解释:返回一个 NestedInteger 对象包含一个有两个元素的嵌套列表:
28+
1. 一个 integer 包含值 123
29+
2. 一个包含两个元素的嵌套列表:
30+
i. 一个 integer 包含值 456
31+
ii. 一个包含一个元素的嵌套列表
32+
a. 一个 integer 包含值 789
33+
```
34+
35+
提示:
36+
* 1ドル <= s.length <= 5 * 10^4$
37+
* `s` 由数字、方括号 `"[]"`、负号 `'-'` 、逗号 `','`组成
38+
用例保证 s 是可解析的 NestedInteger
39+
输入中的所有值的范围是 [-106, 106]
40+
41+
---
42+
43+
###
44+
45+
每个 `[` 的出现意味着存在一个嵌套类型的 `NestedInteger` 实例,同时每个 `NestedInteger` 实例(无论是嵌套类型还是数值类型)都归属于其最近一个左边的嵌套类型的 `NestedInteger` 实例(即其左边最近一个 `[`),因此我们可以使用栈来处理。
46+
47+
对出现的几类字符进行简单分情况讨论:
48+
49+
* `,`:跳过即可;
50+
* `-``数字`:将连续段代表数值的字符串取出,创建数值型的 `NestedInteger` 实例并压入栈中;
51+
* `[`:创建一个嵌套类型的 `NestedInteger` 实例并压入栈中,同时为了方便,同时压入一个起「标识」作用的 `NestedInteger` 对象;
52+
* `]`:从栈中取出元素,直到遇到起「标识」作用的 `NestedInteger` 对象(说明找到与当前 `]` 成对的 `[`),将 `[``]` 之间的所有元素添加到 `[` 所代指的嵌套 `NestedInteger` 实例中,然后将 `[` 所代指的嵌套 `NestedInteger` 实例重新放入栈中。
53+
54+
按照上述逻辑处理完整个 `s`,最终栈顶元素即是答案。
55+
56+
代码:
57+
```Java
58+
class Solution {
59+
static NestedInteger ph = new NestedInteger(0);
60+
public NestedInteger deserialize(String s) {
61+
Deque<NestedInteger> d = new ArrayDeque<>();
62+
char[] cs = s.toCharArray();
63+
int n = cs.length, i = 0;
64+
while (i < n) {
65+
if (cs[i] == ',' && ++i >= 0) continue;
66+
if (cs[i] == '-' || (cs[i] >= '0' && cs[i] <= '9')) {
67+
int j = cs[i] == '-' ? i + 1 : i, num = 0;
68+
while (j < n && (cs[j] >= '0' && cs[j] <= '9')) num = num * 10 + (cs[j++] - '0');
69+
d.addLast(new NestedInteger(cs[i] == '-' ? -num : num));
70+
i = j;
71+
} else if (cs[i] == '[') {
72+
d.addLast(new NestedInteger());
73+
d.addLast(ph);
74+
i++;
75+
} else {
76+
List<NestedInteger> list = new ArrayList<>();
77+
while (!d.isEmpty()) {
78+
NestedInteger poll = d.pollLast();
79+
if (poll == ph) break;
80+
list.add(poll);
81+
}
82+
for (int j = list.size() - 1; j >= 0; j--) d.peekLast().add(list.get(j));
83+
i++;
84+
}
85+
}
86+
return d.peekLast();
87+
}
88+
}
89+
```
90+
* 时间复杂度:$O(n)$
91+
* 空间复杂度:$O(n)$
92+
93+
---
94+
95+
### 最后
96+
97+
这是我们「刷穿 LeetCode」系列文章的第 `No.385` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
98+
99+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
100+
101+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
102+
103+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
104+

0 commit comments

Comments
(0)

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