做网站的目标是什么,学程序员需要什么基础,wordpress商城主题开发,wordpress直播网站主题文章目录1. 题目2. 解题1. 题目
给定一个用字符串表示的整数的嵌套列表#xff0c;实现一个解析它的语法分析器。
列表中的每个元素只可能是整数或整数嵌套列表
提示#xff1a;你可以假定这些字符串都是格式良好的#xff1a;
字符串非空
字符串不包含空格
字符串只包含…
文章目录1. 题目2. 解题1. 题目
给定一个用字符串表示的整数的嵌套列表实现一个解析它的语法分析器。
列表中的每个元素只可能是整数或整数嵌套列表
提示你可以假定这些字符串都是格式良好的
字符串非空
字符串不包含空格
字符串只包含数字0-9、[、-、,、]示例 1
给定 s 324,
你应该返回一个 NestedInteger 对象其中只包含整数值 324。示例 2
给定 s [123,[456,[789]]],
返回一个 NestedInteger 对象包含一个有两个元素的嵌套列表1. 一个 integer 包含值 123
2. 一个包含两个元素的嵌套列表i. 一个 integer 包含值 456ii. 一个包含一个元素的嵌套列表a. 一个 integer 包含值 789来源力扣LeetCode 链接https://leetcode-cn.com/problems/mini-parser 著作权归领扣网络所有。商业转载请联系官方授权非商业转载请注明出处。 2. 解题 /*** // This is the interface that allows for creating nested lists.* // You should not implement it, or speculate about its implementation* class NestedInteger {* public:* // Constructor initializes an empty nested list.* NestedInteger();** // Constructor initializes a single integer.* NestedInteger(int value);** // Return true if this NestedInteger holds a single integer, rather than a nested list.* bool isInteger() const;** // Return the single integer that this NestedInteger holds, if it holds a single integer* // The result is undefined if this NestedInteger holds a nested list* int getInteger() const;** // Set this NestedInteger to hold a single integer.* void setInteger(int value);** // Set this NestedInteger to hold a nested list and adds a nested integer to it.* void add(const NestedInteger ni);** // Return the nested list that this NestedInteger holds, if it holds a nested list* // The result is undefined if this NestedInteger holds a single integer* const vectorNestedInteger getList() const;* };*/
class Solution {
public:NestedInteger deserialize(string s) {if(s[0] ! [)return NestedInteger(stoi(s));stackNestedInteger stk;string num;for(int i 0; i s.size(); i){if(s[i] [)stk.push(NestedInteger());else if(isdigit(s[i]) || s[i]-){num s[i];}else // , ]{if(!num.empty()){stk.top().add(NestedInteger(stoi(num)));num.clear();}if(s[i]] stk.size()1){NestedInteger t stk.top();stk.pop();stk.top().add(t);}}}return stk.top();}
};16 ms 11.2 MB C 我的CSDN博客地址 https://michael.blog.csdn.net/
长按或扫码关注我的公众号Michael阿明一起加油、一起学习进步