Using iterators - not faster than int iterators but that is what is asked - C++


  • 0
    class Vector2D {
    public:
        std::vector<vector<int>>::iterator x;
        std::vector<int>::iterator y;
        vector<vector<int>>vec2d;
    
        Vector2D(vector<vector<int>>& _vec2d) {
            vec2d = _vec2d;
            if(!vec2d.empty()){
                x = vec2d.begin();
                y = x->begin();
            }
        }
    
        int next() {
            return *y++;
        }
    
        bool hasNext() {
            while(x != vec2d.end()){
                while(y!=x->end()){
                    return true;
                }
                x++;
                y=x->begin();
            }
            return false;
        }
    };

Log in to reply
 

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