摊平二维向量 flatten-2d-vector

设计一个迭代器来实现摊平二维向量的功能

flatten-2d-vector

样例

例1:

输入:[[1,2],[3],[4,5,6]]
输出:[1,2,3,4,5,6]

例2:

输入:[[7,9],[5]]
输出:[7,9,5]
思路
使用一个 list 存储即可


public class Vector2D implements Iterator<Integer> {
    private List<Integer> list;
    private int count;


    public Vector2D(List<List<Integer>> vec2d) {
        // Initialize your data structure here
        list = new ArrayList<>();
        for (List<Integer> integers : vec2d) {
            for (Integer item : integers) {
                list.add(item);
            }
        }
    }

    @Override
    public Integer next() {
        // Write your code here
        return list.get(count++);

    }

    @Override
    public boolean hasNext() {
        // Write your code here
        return count < list.size();

    }

    @Override
    public void remove() {
    }
}

/**
 * Your Vector2D object will be instantiated and called as such:
 * Vector2D i = new Vector2D(vec2d);
 * while (i.hasNext()) v[f()] = i.next();
 */

源码地址 https://github.com/xingfu0809/Java-LintCode

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • vector(向量) C++中的一种数据结构,确切的说是一个类。它相当于一个动态的数组,当程序员无法知道自己需要的...
    资深小夏阅读 497评论 0 2
  • 支持向量机:是一种监督式学习的方法,可广泛地应用于统计分类以及回归分析。支持向量机属于一般化线性分类器,这族分类器...
    Vince_zzhang阅读 1,296评论 0 0
  • 转自http://www.cnblogs.com/mr-wid/archive/2013/01/22/287110...
    winng伍寅阅读 656评论 0 1
  • 描述 设计一个迭代器来实现摊平二维向量的功能 样例 给一个二维向量[ [1,2], [3], [4,5,6] ]通...
    6默默Welsh阅读 336评论 0 0
  • 写在前面:这是一篇思量很久却迟迟无法动笔的文章,因为很多情绪和心理上的小细节无法精确表达。姑且写之,姑且看之。 初...
    妹豆阅读 2,886评论 0 2