其实本题没有我想的那么难,就是到很普通的基础题,只需要注意在每次hasNext()的时候,set()一下就成了(也许col outOfIndex)
时间复杂度:
- Constructor:
set():: inner array的数量
:总共的number
- 一共调用
次,真正运行过
次,所以平均时间复杂度是
next()/hasNext(): 既可能是,也可能是
代码如下:
class Vector2D {private int row;private int col;private int[][] arr;public Vector2D(int[][] v) {this.arr = v;this.row = 0;this.col = 0;}private void set() {while (row < arr.length && col == arr[row].length) {col = 0;++row;}}public int next() {if (!hasNext()) {throw new java.util.NoSuchElementException();}return arr[row][col++];}public boolean hasNext() {set();return row < arr.length;}}/*** Your Vector2D object will be instantiated and called as such:* Vector2D obj = new Vector2D(v);* int param_1 = obj.next();* boolean param_2 = obj.hasNext();*/
