C++, using iterators, beats 53%


  • 0
    S
    public:
        vector<vector<int>>::iterator ed;
        vector<vector<int>>::iterator outer;
        vector<int>::iterator inner;
        
        Vector2D(vector<vector<int>>& vec2d) {
            outer = vec2d.begin();
            ed = vec2d.end();
            if(outer!=ed)
                inner = (*outer).begin();
        }
        int next() {
            int value = *inner;
            ++inner;
            return value;
        }
    
        bool hasNext() {
            if(outer==ed)
                return false;
            while(inner == (*outer).end()){
                ++outer;
                if(outer==ed)
                    return false;
                inner = (*outer).begin();
            }
            return true;
        }
    };

Log in to reply
 

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