本文整理汇总了C++中NestedInteger::add方法的典型用法代码示例。如果您正苦于以下问题:C++ NestedInteger::add方法的具体用法?C++ NestedInteger::add怎么用?C++ NestedInteger::add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NestedInteger
的用法示例。
在下文中一共展示了NestedInteger::add方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: impl
NestedInteger impl(const std::string& s, int startIdx, int endIdx)
{
if(s[startIdx] != '[')
{
return NestedInteger(std::stoi(s.substr(startIdx, endIdx - startIdx)));
}
NestedInteger t;
for(int i = startIdx + 1; i < endIdx - 1;)
{
if(s[i] == '[')
{
int j = i + 1;
int cnt = 1;
while(j < endIdx && cnt)
{
if(s[j] == '[') ++cnt;
else if(s[j] == ']') --cnt;
++j;
}
t.add(impl(s, i, j));
i = j + 1;
}
else
{
int j = i;
while(j < endIdx && s[j] != ',') ++j;
t.add(impl(s, i, j));
i = j + 1;
}
}
return t;
}
示例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 & strSrc, int iStartIndex, int iEndIndex)
{
if (strSrc[iStartIndex] != '[')
{
return NestedInteger(atoi(strSrc.substr(iStartIndex, iEndIndex - iStartIndex + 1).c_str()));
}
if (strSrc[iStartIndex + 1] == ']')
{
return NestedInteger();
}
NestedInteger stResult;
for (int iCurrHeadIndex = iStartIndex + 1, iCurrTailIndex = iCurrHeadIndex, iCurrEmbeddedCheck = 0; iCurrTailIndex <= iEndIndex; iCurrTailIndex ++)
{
if (strSrc[iCurrHeadIndex] != '[')
{
if (strSrc[iCurrTailIndex] == ',' || iCurrTailIndex == iEndIndex)
{
stResult.add(NestedInteger(atoi(strSrc.substr(iCurrHeadIndex, iCurrTailIndex - iCurrHeadIndex + 1).c_str())));
iCurrHeadIndex = iCurrTailIndex + 1;
}
}
else
{
if (iCurrEmbeddedCheck == 0 && (strSrc[iCurrTailIndex] == ',' || iCurrTailIndex == iEndIndex))
{
stResult.add(deserialize(strSrc, iCurrHeadIndex, iCurrTailIndex - 1));
iCurrHeadIndex = iCurrTailIndex + 1;
}
else if (strSrc[iCurrTailIndex] == '[')
{
iCurrEmbeddedCheck ++;
}
else if (strSrc[iCurrTailIndex] == ']')
{
iCurrEmbeddedCheck --;
}
else
{
}
}
}
return stResult;
}
示例4: 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;
}
}
示例5: deserialize
NestedInteger deserialize(string s) {
if (s.empty()) return NestedInteger(); // empty list
if (s[0] == '[') { // nested list
NestedInteger n;
int i = 1, end = s.size() - 1;
while (i < end) {
int begin = i;
if (s[i] == '[') { // element being nested list
int cnt = 0;
do {
if (s[i] == '[') ++cnt;
else if (s[i] == ']') --cnt;
++i;
} while (cnt > 0);
} else {
while (isdigit(s[i]) || s[i] == '-') ++i;
}
n.add(deserialize(s.substr(begin, i - begin)));
++i;
}
return n;
}
// plain number
return NestedInteger(stoi(s));
}
示例6: main
int main() {
NestedInteger nl;
#if 0
// empty test case
#endif
#if 0
NestedInteger first;
first.add(NestedInteger(1));
first.add(NestedInteger(1));
NestedInteger second(2);
NestedInteger third {first};
nl.add(first);
nl.add(second);
nl.add(third);
nl.print(); cout << endl;
#endif
#if 1
NestedInteger first;
first.add(NestedInteger(1));
NestedInteger second;
second.add(NestedInteger(4));
NestedInteger third;
third.add(NestedInteger(6));
second.add(third);
first.add(second);
nl.add(first);
nl.print(); cout << endl;
#endif
NestedIterator i(nl.getList());
while (i.hasNext()) cout << i.next();
cout << endl;
return 0;
}
示例7: deserialize
NestedInteger deserialize(istringstream &in) {
int number;
if(in>>number) return NestedInteger(number); //如果开头是数字,那么一定是单个数的
in.clear();
in.get();
NestedInteger list;
while(in.peek() != ']')
{
list.add(deserialize(in));
if(in.peek()==',')
in.get();
}
in.get();
return list;
}