NestedInteger deserialize(string s) { num = 0; isNeg = false; stk.push(NestedInteger()); for (auto ch: s) { switch (ch) { case '[': { stk.push(NestedInteger()); break; } case ']': { finishNum(); NestedInteger nestNum = stk.top(); stk.pop(); stk.top().add(nestNum); break; } case ',': { finishNum(); break; } case '-': { isNeg = true; break; } default: { hasNum = true; num = num * 10 + int(ch - '0'); } } } finishNum(); return stk.top().getList()[0]; }
NestedInteger deserialize(string s) { if(s[0] != '[') return NestedInteger(stoi(s)); stack<NestedInteger*> stk; NestedInteger* ans = NULL; int idx = 0; for(int i=0; i<s.size(); i++) { if(s[i] == '[') { stk.push(new NestedInteger()); if(!ans) ans = stk.top(); idx = i + 1; } else if(s[i] == ',') { if(idx != i) stk.top()->add(NestedInteger(stoi(s.substr(idx, i-idx)))); idx = i + 1; } else if(s[i] == ']') { if(idx != i) stk.top()->add(NestedInteger(stoi(s.substr(idx, i-idx)))); NestedInteger* cur = stk.top(); stk.pop(); if(!stk.empty()) stk.top()->add(*cur); idx = i + 1; } } return *ans; }
NestedInteger deserialize(string s) { function<bool(char)> isnumber = [](char c){ return (c == '-') || isdigit(c); }; stack<NestedInteger> stk; stk.push(NestedInteger()); for (auto it = s.begin(); it != s.end();) { const char & c = (*it); if (isnumber(c)) { auto it2 = find_if_not(it, s.end(), isnumber); int val = stoi(string(it, it2)); stk.top().add(NestedInteger(val)); it = it2; } else { if (c == '[') { stk.push(NestedInteger()); } else if (c == ']') { NestedInteger ni = stk.top(); stk.pop(); stk.top().add(ni); } ++it; } } NestedInteger result = stk.top().getList().front(); return result; }
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)); }
NestedInteger deserialize(string s) { int num = 0; int idx = 0; if(s[0] != '[') return NestedInteger(stoi(s)); int sign = 1; for(int i = 0; i < s.length(); ++i) { char c = s[i]; if(c == '[') { nstk.push(NestedInteger()); continue; } else if(c == ']' && nstk.size() > 1) { auto temp = nstk.top(); nstk.pop(); nstk.top().add(temp); } else if(c == ',') { continue; } else if(c == '-') sign = -1; else if(c >= '0' && c <= '9'){ num = c - '0'; while(i + 1 < s.length() && s[i + 1] >= '0' && s[i + 1] <= '9' ) num = num * 10 + (s[(i++) + 1] - '0'); num *= sign; if(!nstk.empty()) nstk.top().add(NestedInteger(num)); else nstk.push(NestedInteger(num)); sign = 1; } } return nstk.empty()? NestedInteger(): nstk.top(); }
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; }
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; }
void finishNum() { if (hasNum) { stk.top().add(NestedInteger(isNeg ? -num : num)); hasNum = false; isNeg = false; num = 0; } }
NestedInteger deserialize(string s) { if (s == "") return NestedInteger(); istringstream in(s); return deserializeHelper(in); }