Unexpected run time error happened. Can anyone help me to find the bug?

  • 0

    I get runtime error with my solution. I have spent a lot of time on debugging however I just cannot see why runtime error happened. Since the source code of methods (isInteger(), getInteger, getList()) is not provided, I cannot debug with XCode by myself.

    curIter and endIter record the current iterator and the end iterator of the vector that I am processing. I will make it a pair and push it into the stack when curIter points to a list instead of an integer. And pop the stack when curIter reaches endIter.

    After debugging, I found that the condition "if(curIter == endIter)" in findNextinteger() doesn't satisfy in the time that I think if should such that the system cannot be terminated when calling hasNext().

    I will be very much appreciate if anyone could help me with this problem. Thanks.

        stack<pair<vector<NestedInteger>::iterator , vector<NestedInteger>::iterator>> nestedStack;
        vector<NestedInteger>::iterator curIter, endIter;
        NestedIterator(vector<NestedInteger> &nestedList) {
            if(nestedList.empty())  return;
            curIter = nestedList.begin();
            endIter = nestedList.end();
        int next() {
            int rtn = curIter->getInteger();
            return rtn;
        bool hasNext() {
            return !(nestedStack.empty() && (curIter == endIter));
        void findNextInteger(){
            if(curIter == endIter){
                    curIter = nestedStack.top().first + 1;
                    endIter = nestedStack.top().second;
            else if(!curIter->isInteger()){
                nestedStack.push(make_pair(curIter, endIter));
                curIter = curIter->getList().begin();
                endIter = curIter->getList().end();

Log in to reply

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