首页 > 其他分享 >251. Flatten 2D Vector 平铺矩阵

251. Flatten 2D Vector 平铺矩阵

时间:2022-11-13 07:11:06浏览次数:57  
标签:return iterator Vector2D next 2D Vector Flatten hasNext public

Design and implement an iterator to flatten a 2d vector. It should support the following operations: next and hasNext.

 

Example:

Vector2D iterator = new Vector2D([[1,2],[3],[4]]);

iterator.next(); // return 1
iterator.next(); // return 2
iterator.next(); // return 3
iterator.hasNext(); // return true
iterator.hasNext(); // return true
iterator.next(); // return 4
iterator.hasNext(); // return false

public class Vector2D {

    private Iterator<List<Integer>> i;
    private Iterator<Integer> j;

    public Vector2D(List<List<Integer>> vec2d) {
        i = vec2d.iterator();
    }

    public int next() {
        hasNext();
        return j.next();
    }

    public boolean hasNext() {
        while ((j == null || !j.hasNext()) && i.hasNext())
            j = i.next().iterator();
        return j != null && j.hasNext();
    }
}

 

 

标签:return,iterator,Vector2D,next,2D,Vector,Flatten,hasNext,public
From: https://www.cnblogs.com/immiao0319/p/16885338.html

相关文章