开启我的LeetCode刷题日记:385. 迷你语法分析器

67 阅读3分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第1天,点击查看活动详情

编程世界总是离不了算法

最近在看框架源码时,会有很多算法的实现逻辑,有时候会感到吃力

于是决定蹭着假期,加强算法和数据结构相关的知识

那怎么提升呢?

其实我知道算法这东西没有捷径,多写多练才能提升,于是我开启我的LeetCode刷题之旅

第一阶段目标是:200道,每天12

为了不乱,本系列文章目录分为三部分:

  1. 今日题目:xxx
  2. 我的思路
  3. 代码实现

今日题目:385. 迷你语法分析器

给定一个字符串 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 * 104
s 由数字、方括号 "[]"、负号 '-' 、逗号 ','组成
用例保证 s 是可解析的 NestedInteger
输入中的所有值的范围是 [-106, 106]

来源:力扣(LeetCode) 链接:leetcode.cn/problems/mi… 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

我的思路

  • 遍历字符串,如果遇到 [ 则直接 new NestedInteger(),并存储到栈中
  • 如果遇到 ] 或者 , 表示已存储的 num 遇到截止字符
    • 如果 num 不为空,则将 num 通过 new NestedInteger() 实例化并存储到栈顶,然后清空 num
    • 如果遇到的是 ] 字符,需要判断当前栈的长度是否 >= 2,因为需要将栈顶元素 add 到次栈顶元素内
  • 如果不是上述两种情况,则表示遇到的是数字或者 -,直接拼接到 num 字符串中 遍历完成后,如果栈的长度为 1 则返回栈顶元素,否为直接返回 new NestedInteger(num) 即可。

代码实现

/**
 * // This is the interface that allows for creating nested lists.
 * // You should not implement it, or speculate about its implementation
 * function NestedInteger() {
 *
 *     Return true if this NestedInteger holds a single integer, rather than a nested list.
 *     @return {boolean}
 *     this.isInteger = function() {
 *         ...
 *     };
 *
 *     Return the single integer that this NestedInteger holds, if it holds a single integer
 *     Return null if this NestedInteger holds a nested list
 *     @return {integer}
 *     this.getInteger = function() {
 *         ...
 *     };
 *
 *     Set this NestedInteger to hold a single integer equal to value.
 *     @return {void}
 *     this.setInteger = function(value) {
 *         ...
 *     };
 *
 *     Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
 *     @return {void}
 *     this.add = function(elem) {
 *         ...
 *     };
 *
 *     Return the nested list that this NestedInteger holds, if it holds a nested list
 *     Return null if this NestedInteger holds a single integer
 *     @return {NestedInteger[]}
 *     this.getList = function() {
 *         ...
 *     };
 * };
 */
/**
 * @param {string} s
 * @return {NestedInteger}
 */
var deserialize = function(s) {
    const stack = new Stack();
    let num = '';
    for(const char of s) {
        if (char === '[') {
            stack.push(new NestedInteger());
        } else if ([']', ','].includes(char)) {
            // 如果 num 存在,则将 num 存储到栈顶元素中并清空 num
            if (num) {
                 stack.top().add(new NestedInteger(num));
                 num = '';
            }
            // 如果为 ],则将栈顶元素 add 到栈顶倒数第二个元素中
            if (char === ']' && stack.count >= 2) {
                const temp = stack.pop();
                stack.top().add(temp);
            }
        } else {
            // 为数字
            num += char;
        }
        // console.log(num);
    }
    return stack.count === 1 ? stack.pop() : new NestedInteger(num);
};

var Stack = function() {
    this.stack = [];
    this.count = 0
}

Stack.prototype.push = function(s) {
    this.stack[this.count++] = s;
}

Stack.prototype.top = function() {
    return this.stack[this.count - 1];
}

Stack.prototype.pop = function() {
    this.count--;
    return this.stack.pop();
}

Stack.prototype.isEmpty = function() {
    return this.count === 0;
}


总结

实现方式其实有很多,这里仅供参考~

由于刚开始刷题,也不知道从哪里刷好,如果前辈们有好的建议,希望不吝赐教,感谢🌹