C++_Accepted_vector_by using bit manipulation


  • 1

    22ms, 69.34%

    class ZigzagIterator {
    vector<vector<int>> nums;
    int rows = 0, cols = 0;
    public:
    ZigzagIterator(vector<int>& v1, vector<int>& v2) {
        nums.push_back(v1);
        nums.push_back(v2);
    }
    
    int next() {
        int res = nums[rows][cols];
        cols = cols + rows;
        rows = !rows;
        return res;
    }
    
    bool hasNext() {
        if(cols < nums[rows].size()){return true;}
        else if(cols+rows < nums[!rows].size()){cols += rows, rows = !rows; return true;}
        return false;
    }
    };
    

    0_1477594424069_B5A750DE-5F52-40EE-9BE7-C14C1FC2300B.png


Log in to reply
 

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