zoukankan      html  css  js  c++  java
  • 251. Flatten 2D Vector

    package LeetCode_251
    
    /**
     * 251. Flatten 2D Vector
     * (Prime)
     * 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
    
    Notes:
    1. Please remember to RESET your class variables declared in Vector2D,
    as static/class variables are persisted across multiple test cases. Please see here for more details.
    2. You may assume that next() call will always be valid, that is,
    there will be at least a next element in the 2d vector when next() is called.
     * */
    
    /*
    * Solution: change 2d array into 1d array
    * */
    class Vector2D(twoDArray: Array<IntArray>) {
    
        var array: IntArray? = null
        var index = 0
        var size = 0
    
        init {
            //calculate the size of 1d array
            for (array in twoDArray) {
                size += array.size
            }
            array = IntArray(size)
            //fill array
            var i = 0
            for (array_ in twoDArray) {
                for (num in array_) {
                    array!![i++] = num
                }
            }
        }
    
        fun next(): Int {
            return array!![index++]
        }
    
        fun hasNext(): Boolean {
            return index < size
        }
    }
  • 相关阅读:
    DecimalFormat
    flex 分页
    flex 分页
    算法学习——st表
    [USACO07DEC]美食的食草动物Gourmet Grazers
    [ZJOI2005]沼泽鳄鱼 矩阵乘法
    [SCOI2010]序列操作 线段树
    [LNOI2014]LCA
    [AHOI2013]作业 & Gty的二逼妹子序列 莫队
    Linux相关——关于文件调用
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13569030.html
Copyright © 2011-2022 走看看