本文整理汇总了C++中NestedInteger::setInteger方法的典型用法代码示例。如果您正苦于以下问题:C++ NestedInteger::setInteger方法的具体用法?C++ NestedInteger::setInteger怎么用?C++ NestedInteger::setInteger使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NestedInteger
的用法示例。
在下文中一共展示了NestedInteger::setInteger方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: deserializeRecursively
NestedInteger deserializeRecursively(string &s, int& pos) {
if (s[pos] == '[') {
NestedInteger n;
pos++;
while (s[pos] != ']') {
n.add(deserializeRecursively(s, pos));
}
pos++;
return n;
}
else if (isdigit(s[pos]) || s[pos] == '-') {
int res = 1;
int sign = 1;
if (s[pos] == '-') {
pos++;
sign = -1;
}
while (s[pos] != ',' && s[pos] != '\0') {
res = res * 10 + s[pos] - '0';
pos++;
}
NestedInteger n;
n.setInteger(res * sign);
pos++;
return n;
}
}
示例2: helper
NestedInteger helper(string& s, int start, int end) {
NestedInteger ni;
int nestedStart = -1;
int n = 0;
int num = 0;
if (s[start] != '[') {
bool neg = false;
for (int i = start; i <= end; i++) {
if (s[i] == '-') {
neg = true;
} else {
num = 10 * num + (s[i] - '0');
}
}
if (neg == true) num *= -1;
ni.setInteger(num);
} else {
start++;
end--;
int lastStart = start;
for (int i = start ; i <= end; i++) {
if (s[i] == '[') {
n++;
if (nestedStart == -1) nestedStart = i;
} else if (s[i] == ']') {
n--;
} else if (s[i] == ',' && n == 0) {
ni.add(helper(s, lastStart, i - 1));
lastStart = i + 1;
}
if (i == end) ni.add(helper(s, lastStart, end));
}
}
return ni;
}
示例3: deserialize
NestedInteger deserialize(string s) {
NestedInteger *cur;
for(int i = 0; i < s.size(); ++i) {
if(s[i] == ',') continue;
cur = new NestedInteger;
if(s[i] == '[') candidate.push(new NestedInteger());
else if(s[i] == ']') {
cur = candidate.top();
candidate.pop();
if(!candidate.empty()) candidate.top()->add(*cur);
} else {
int j = i;
while(j < s.size() && s[j] != ',' && s[j] != ']') ++j;
cur->setInteger(stoi(s.substr(i, j - i)));
if(!candidate.empty()) candidate.top()->add(*cur);
i = j - 1;
}
}
return candidate.empty() ? *cur : *candidate.top();
}