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


  • 0
    W

    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.

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

Log in to reply
 

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