C++ code with 36ms


  • 1
    S
    class Solution{
          stack<NestedInteger*> Stack;
    public:
          NestedInteger<string s>{
                 NestedInteger *cur;
                 for(int i=0;i<s.size();i++){
                      if(s[i]==',')continue;
                      cur=new NestedInteger();
                      if(s[i]=='[')Stack.push(new NestedInteger());
                      else if(s[i]==']'){
                            cur=Stack.top();
                            Stack.pop();
                            if(!Stack.empty())Stack.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(!Stack.empty())Stack.top()->add(*cur);
                            i=j-1;
                     }
               }
               return Stack.empty()? *cur:*Stack.top();
          }
      };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.