diff --git "a/Index/\346\240\210.md" "b/Index/\346\240\210.md" index 67e7bcbd..a33a5582 100644 --- "a/Index/\346\240\210.md" +++ "b/Index/\346\240\210.md" @@ -6,6 +6,7 @@ | [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/) | 简单 | 🤩🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩 | +| [385. 迷你语法分析器](https://leetcode-cn.com/problems/mini-parser/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/mini-parser/solution/by-ac_oier-zuy6/) | 中等 | 🤩🤩🤩🤩🤩 | | [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/) | 困难 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩🤩 | | [面试题 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/) | 简单 | 🤩🤩🤩 | diff --git "a/Index/\346\250\241\346\213\237.md" "b/Index/\346\250\241\346\213\237.md" index d6cdd56e..0c3bd4b0 100644 --- "a/Index/\346\250\241\346\213\237.md" +++ "b/Index/\346\250\241\346\213\237.md" @@ -40,6 +40,7 @@ | [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/) | 简单 | 🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩🤩 | +| [385. 迷你语法分析器](https://leetcode-cn.com/problems/mini-parser/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/mini-parser/solution/by-ac_oier-zuy6/) | 中等 | 🤩🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | diff --git "a/LeetCode/381-390/385. \350\277\267\344\275\240\350\257\255\346\263\225\345\210\206\346\236\220\345\231\250\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/381-390/385. \350\277\267\344\275\240\350\257\255\346\263\225\345\210\206\346\236\220\345\231\250\357\274\210\344\270\255\347\255\211\357\274\211.md" new file mode 100644 index 00000000..012fe4fa --- /dev/null +++ "b/LeetCode/381-390/385. \350\277\267\344\275\240\350\257\255\346\263\225\345\210\206\346\236\220\345\231\250\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -0,0 +1,104 @@ +### 题目描述 + +这是 LeetCode 上的 **[385. 迷你语法分析器](https://leetcode-cn.com/problems/mini-parser/solution/by-ac_oier-zuy6/)** ,难度为 **中等**。 + +Tag : 「栈」、「模拟」 + + + +给定一个字符串 `s` 表示一个整数嵌套列表,实现一个解析它的语法分析器并返回解析的结果 `NestedInteger` 。 + +列表中的每个元素只可能是整数或整数嵌套列表 + +示例 1: +``` +输入:s = "324", + +输出:324 + +解释:你应该返回一个 NestedInteger 对象,其中只包含整数值 324。 +``` +示例 2: +``` +输入:s = "[123,[456,[789]]]", + +输出:[123,[456,[789]]] + +解释:返回一个 NestedInteger 对象包含一个有两个元素的嵌套列表: +1. 一个 integer 包含值 123 +2. 一个包含两个元素的嵌套列表: + i. 一个 integer 包含值 456 + ii. 一个包含一个元素的嵌套列表 + a. 一个 integer 包含值 789 +``` + +提示: +* $1 <= s.length <= 5 * 10^4$ +* `s` 由数字、方括号 `"[]"`、负号 `'-'` 、逗号 `','`组成 +用例保证 s 是可解析的 NestedInteger +输入中的所有值的范围是 [-106, 106] + +--- + +### 栈 + +每个 `[` 的出现意味着存在一个嵌套类型的 `NestedInteger` 实例,同时每个 `NestedInteger` 实例(无论是嵌套类型还是数值类型)都归属于其最近一个左边的嵌套类型的 `NestedInteger` 实例(即其左边最近一个 `[`),因此我们可以使用栈来处理。 + +对出现的几类字符进行简单分情况讨论: + +* `,`:跳过即可; +* `-` 或 `数字`:将连续段代表数值的字符串取出,创建数值型的 `NestedInteger` 实例并压入栈中; +* `[`:创建一个嵌套类型的 `NestedInteger` 实例并压入栈中,同时为了方便,同时压入一个起「标识」作用的 `NestedInteger` 对象; +* `]`:从栈中取出元素,直到遇到起「标识」作用的 `NestedInteger` 对象(说明找到与当前 `]` 成对的 `[`),将 `[` 和 `]` 之间的所有元素添加到 `[` 所代指的嵌套 `NestedInteger` 实例中,然后将 `[` 所代指的嵌套 `NestedInteger` 实例重新放入栈中。 + +按照上述逻辑处理完整个 `s`,最终栈顶元素即是答案。 + +代码: +```Java +class Solution { + static NestedInteger ph = new NestedInteger(0); + public NestedInteger deserialize(String s) { + Deque d = new ArrayDeque<>(); + char[] cs = s.toCharArray(); + int n = cs.length, i = 0; + while (i < n) { + if (cs[i] == ',' && ++i >= 0) continue; + if (cs[i] == '-' || (cs[i] >= '0' && cs[i] <= '9')) { + int j = cs[i] == '-' ? i + 1 : i, num = 0; + while (j < n && (cs[j] >= '0' && cs[j] <= '9')) num = num * 10 + (cs[j++] - '0'); + d.addLast(new NestedInteger(cs[i] == '-' ? -num : num)); + i = j; + } else if (cs[i] == '[') { + d.addLast(new NestedInteger()); + d.addLast(ph); + i++; + } else { + List list = new ArrayList<>(); + while (!d.isEmpty()) { + NestedInteger poll = d.pollLast(); + if (poll == ph) break; + list.add(poll); + } + for (int j = list.size() - 1; j >= 0; j--) d.peekLast().add(list.get(j)); + i++; + } + } + return d.peekLast(); + } +} +``` +* 时间复杂度:$O(n)$ +* 空间复杂度:$O(n)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.385` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +