Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
mini-parser.cpp 3.72 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2016-08-15 00:35 +08:00 . Update mini-parser.cpp
// Time: O(n)
// Space: O(h)
/**
* // 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;
* };
*/
// Iterative solution.
class Solution {
public:
NestedInteger deserialize(string s) {
if (s.empty()) {
return NestedInteger();
}
if (s[0] != '[') {
return NestedInteger(stoi(s));
}
stack<NestedInteger> stk;
for (int i = 0, j = 0; j < s.length(); ++j) {
if (s[j] == '[') {
stk.emplace(NestedInteger());
i = j + 1;
} else if (s[j] == ',' || s[j] == ']') {
if (isdigit(s[j - 1])) {
stk.top().add(NestedInteger(stoi(s.substr(i, j - i))));
}
if (s[j] == ']' && stk.size() > 1) {
NestedInteger cur = stk.top();
stk.pop();
stk.top().add(cur);
}
i = j + 1;
}
}
return stk.top();
}
};
// Time: O(n)
// Space: O(h)
// Recursive solution.
class Solution2 {
public:
NestedInteger deserialize(string s) {
if (s.empty()) {
return NestedInteger();
}
int i = 0;
return deserializeHelper(s, &i);
}
private:
NestedInteger deserializeHelper(const string& s, int *i) {
NestedInteger result;
if (s[*i] != '[') {
int j = *i;
while (j < s.length() && (s[j] == '-' || isdigit(s[j]))) {
++j;
}
result.setInteger(stoi(s.substr(*i, j - *i + 1)));
*i = j;
} else {
++(*i);
while (*i < s.length() && s[*i] != ']') {
result.add(deserializeHelper(s, i));
if (*i < s.length() && s[*i] == ',') {
++(*i);
}
}
++(*i);
}
return result;
}
};
// Time: O(n)
// Space: O(n)
// Recursive solution.
class Solution3 {
public:
NestedInteger deserialize(string s) {
if (s.empty()) {
return NestedInteger();
}
istringstream in(s); // copy string: extra O(n) space
return deserializeHelper(in);
}
private:
NestedInteger deserializeHelper(istringstream &in) {
NestedInteger result;
int num = 0;
if (in >> num) {
result.setInteger(num);
} else {
in.clear();
in.get();
while (in.peek() != ']') {
result.add(deserializeHelper(in));
if (in.peek() == ',') {
in.get();
}
}
in.get();
}
return result;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助