本文已参与「新人创作礼」活动,一起开启掘金创作之路。
题目
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
数据规模
提示:
s由数字、方括号"[]"、负号'-'、逗号','组成- 用例保证
s是可解析的NestedInteger - 输入中的所有值的范围是
思路
这题题意莫名奇妙的,原因在于题目并没有解释NestedInteger的定义,全在注释里面,看着很别扭。
NestedInteger实例只能包含下列两部分之一:
- 一个整数;
- 一个列表,列表中的每个元素都是一个
NestedInteger实例。
NestedInteger是可以通过递归定义的,因此也可以用递归的方式来解析。
遍历s:
- 如果第一个是
[,那么就是一个带解析的列表,内部是一个NestedInteger实例,调用解析函数deserialize来解析列表的元素,调用结束后如果遇到的是,,表示列表仍有其他元素,需要继续调用。如果是],表示这个列表已经解析完毕。 - 否则
NestedInteger只包含一个整数。注意判断负数。
代码
// short int long float double bool char string void
// array vector stack queue auto const operator
// class public private static friend extern
// sizeof new delete return cout cin memset malloc
// relloc size length memset malloc relloc size length
// for while if else switch case continue break system
// endl reverse sort swap substr begin end iterator
// namespace include define NULL nullptr exit equals
// index col row arr err left right ans res vec que sta
// state flag ch str max min default charray std
// maxn minn INT_MAX INT_MIN push_back insert
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int>PII;
typedef pair<int, string>PIS;
const int maxn=1e6+50;//注意修改大小
long long read(){long long x=0,f=1;char c=getchar();while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}while(isdigit(c)){x=x*10+c-'0';c=getchar();}return x*f;}
ll qpow(ll x,ll q,ll Mod){ll ans=1;while(q){if(q&1)ans=ans*x%Mod;q>>=1;x=(x*x)%Mod;}return ans%Mod;}
/**
* // 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:
int index=0;
NestedInteger deserialize(string s) {
if(s[index]=='['){
index++;
NestedInteger t;
while(s[index]!=']'){
t.add(deserialize(s));
if(s[index]==',')index++;
}
index++;
return t;
}
else{
int sign=1,t=0;
if(s[index]=='-')sign=-1,index++;
while(index<s.size()&&isdigit(s[index])){
t=t*10+s[index]-'0';
index++;
}
t*=sign;
return NestedInteger(t);
}
}
};