[leetcode] 251. Flatten 2D Vector 解题报告

题目链接: https://leetcode.com/problems/flatten-2d-vector/

Implement an iterator to flatten a 2d vector.

For example,
Given 2d vector =

[
  [1,2],
  [3],
  [4,5,6]
]

By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,2,3,4,5,6].

Hint:

  1. How many variables do you need to keep track?
  2. Two variables is all you need. Try with x and y.
  3. Beware of empty rows. It could be the first few rows.
  4. To write correct code, think about the invariant to maintain. What is it?
  5. The invariant is x and y must always point to a valid point in the 2d vector. Should you maintain your invariant ahead of time or right when you need it?
  6. Not sure? Think about how you would implement hasNext(). Which is more complex?
  7. Common logic in two different places should be refactored into a common method.

Follow up:
As an added challenge, try to code it using only iterators in C++ or iterators in Java.


思路: 写了一个维持两个变量的版本, 感觉太复杂, 然后又看别人怎么用迭代器实现的, 两种代码各自如下:

1. 

class Vector2D {
public:
    Vector2D(vector<vector<int>>& vec2d):vec(vec2d) {
        x=0, y=-1;
        while(vec2d.size()>0 && x < vec2d.size())
        {
            if(vec2d[x].size()>0)
            {
                x = x, y = 0;
                break;
            }
            else x++;
        }
        if(x >= vec2d.size()) x = -1;
    }

    int next() {
        int val = vec[x][y];
        while(x < vec.size())
        {
            if(y+1 < vec[x].size())
            {
                y++;
                break;
            }
            else
            {
                x++;
                while(x < vec.size())
                {
                    if(vec[x].size() > 0)
                    {
                        y = -1;
                        break;
                    }
                    else x++;
                }
            }
        }
        if(x >= vec.size()) x = -1;
        return val;
    }

    bool hasNext() {
        if(x ==-1) return false;
        return true;
    }
private:
    int x, y;
    vector<vector<int>>& vec;
};

/**
 * Your Vector2D object will be instantiated and called as such:
 * Vector2D i(vec2d);
 * while (i.hasNext()) cout << i.next();
 */


2. 

class Vector2D {
public:
    Vector2D(vector<vector<int>>& vec2d) {
        vS = vec2d.begin();
        vE = vec2d.end();
        s = e;
    }

    int next() {
       int val = *s;
       s++;
       return val;
    }

    bool hasNext() {
        while(vS != vE && s == e)
        {
            s = (*vS).begin();
            e = (*vS).end();
            vS++;
        }
        return !(s == e);
    }
private:
    vector<vector<int>>::iterator vS;
    vector<vector<int>>::iterator vE;
    vector<int>::iterator s;
    vector<int>::iterator e;
};

/**
 * Your Vector2D object will be instantiated and called as such:
 * Vector2D i(vec2d);
 * while (i.hasNext()) cout << i.next();
 */
第二种参考: https://leetcode.com/discuss/89400/c-solution-with-only-iterators


你可能感兴趣的:(LeetCode,design)