C++ solution concise


  • 0
    M
    class Vector2D {
    public:
        vector<vector<int>> result;
        int row = 0, col = 0;
        Vector2D(vector<vector<int>>& vec2d) {
            result = vec2d;
        }
    
        int next() {
            return result[row][col++];
        }
    
        bool hasNext() {
            if(result.size()==0)
                return false;
            while(result[row].size()==0){
                row++;
            }
            if(result.size()-1 >= row && result[row].size()-1>=col){
                return true;
            }
            else if(result.size()-1 > row && result[row].size()-1 < col){
                col = 0;
                row++;
                while(result[row].size()==0){
                    row++;
                    if(result.size()-1 < row)
                        return false;
                }
                return true;
            }
            else
                return false;
        }
    };

Log in to reply
 

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