原题地址:https://leetcode-cn.com/problems/mini-parser/
题目描述:
给定一个用字符串表示的整数的嵌套列表,实现一个解析它的语法分析器。
列表中的每个元素只可能是整数或整数嵌套列表
提示:你可以假定这些字符串都是格式良好的:
字符串非空
字符串不包含空格
字符串只包含数字0-9, [, - ,, ]
示例 1:
给定 s = "324",
你应该返回一个 NestedInteger 对象,其中只包含整数值 324。
示例 2:
给定 s = "[123,[456,[789]]]",
返回一个 NestedInteger 对象包含一个有两个元素的嵌套列表:
1. 一个 integer 包含值 123
2. 一个包含两个元素的嵌套列表:
i. 一个 integer 包含值 456
ii. 一个包含一个元素的嵌套列表
a. 一个 integer 包含值 789
解题方案:
首先采用的是递归方法,代码比价简介并容易想到:
/**
* // 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 vector<NestedInteger> &getList() const;
* };
*/
class Solution {
public:
NestedInteger deserialize(string s) {
if(s.empty())
return NestedInteger();
if(s[0] != '[')
return NestedInteger(stoi(s));
if(s.size() <= 2)
return NestedInteger();
NestedInteger res;
int start = 1, cnt = 0;
for (int i =1 ; i < s.size(); i ++){
if(cnt == 0 && (s[i] == ',' || i == s.size() - 1)){
res.add(deserialize(s.substr(start, i - start)));
start = i + 1;
}
else if(s[i] == '[')
cnt ++;
else if(s[i] == ']')
cnt --;
}
return res;
}
};
采用非递归的方法,需要使用栈:
/**
* // 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 vector<NestedInteger> &getList() const;
* };
*/
class Solution {
public:
NestedInteger deserialize(string s) {
if(s.empty())
return NestedInteger();
if(s[0] != '[')
return NestedInteger(stoi(s));
stack<NestedInteger> res;
int start = 1;
for(int i = 0; i < s.size(); i ++){
if(s[i] == '[') {
res.push(NestedInteger());
start = i + 1;
}
else if(s[i]==',' || s[i] == ']'){
if(i > start)
res.top().add(stoi(s.substr(start, i - start)));
start = i + 1;
if(s[i] == ']') {
if(res.size() > 1) {
NestedInteger temp = res.top();res.pop();
res.top().add(temp);
}
}
}
}
return res.top();
}
};
时间最短方案:
*
* @lc app=leetcode.cn id=385 lang=cpp
*
* [385] 迷你语法分析器
*
* https://leetcode-cn.com/problems/mini-parser/description/
*
* algorithms
* Medium (33.94%)
* Total Accepted: 338
* Total Submissions: 996
* Testcase Example: '"324"'
*
* 给定一个用字符串表示的整数的嵌套列表,实现一个解析它的语法分析器。
*
* 列表中的每个元素只可能是整数或整数嵌套列表
*
* 提示:你可以假定这些字符串都是格式良好的:
*
*
* 字符串非空
* 字符串不包含空格
* 字符串只包含数字0-9, [, - ,, ]
*
*
*
*
* 示例 1:
*
*
* 给定 s = "324",
*
* 你应该返回一个 NestedInteger 对象,其中只包含整数值 324。
*
*
*
*
* 示例 2:
*
*
* 给定 s = "[123,[456,[789]]]",
*
* 返回一个 NestedInteger 对象包含一个有两个元素的嵌套列表:
*
* 1. 一个 integer 包含值 123
* 2. 一个包含两个元素的嵌套列表:
* i. 一个 integer 包含值 456
* ii. 一个包含一个元素的嵌套列表
* a. 一个 integer 包含值 789
*
*
*
*
*/
/**
* // 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 vector<NestedInteger> &getList() const;
* };
*/
class Solution
{
public:
NestedInteger deserialize(string s)
{
if (s[0] != '[')
return NestedInteger(stoi(s));
int start;
stack<NestedInteger> res;
for (int i = 0; i < s.size(); ++i)
{
if (s[i] == '[')
{
res.push(NestedInteger());
start = i + 1;
}
else if (s[i] == ']' or s[i] == ',')
{
if (start < i)
res.top().add(stoi(s.substr(start, i - start)));
start = i + 1;
if (s[i] == ']' and res.size() > 1)
{
NestedInteger temp = res.top();
res.pop();
res.top().add(temp);
}
}
}
return res.top();
}
};