[C++ fast] Concise and understandable solution


  • 0
    H
    class Vector2D {
    private:
        int oidx, iidx;
        vector<vector<int>> data;
        
    public:
        Vector2D(vector<vector<int>>& vec2d) : oidx(0), iidx(0), data(vec2d) { }
    
        int next() {
            if (hasNext()) return data[oidx][iidx++];
            else return -1;
        }
    
        bool hasNext() {
            while (oidx < data.size()) {
                if (data[oidx].size() == iidx) {
                    ++oidx;
                    iidx = 0;
                } else break;
            }
            return oidx != data.size();
        }
    };
    

Log in to reply
 

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