★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-xtzbilfx-mb.html
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Implement an iterator to flatten a 2d vector.java
For example,
Given 2d vector =git
[ [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]
.github
Hint:api
x
and y
.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?hasNext()
. Which is more complex?Follow up:
As an added challenge, try to code it using only iterators in C++ or iterators in Java.微信
实现迭代器以展平二维向量。oracle
例如,编码
给定的二维矢量=spa
[ [1,2], [3], [4,5,6] ]
经过重复调用next直到hasnext返回false,next返回的元素顺序应该是:[1,2,3,4,5,6]。code
提示:
跟进:
做为一个额外的挑战,尝试用C++中的迭代器或Java中的迭代器对其进行编码。
1 class WordDistance { 2 var x:Int 3 var y:Int 4 var v:[[Int]] = [[Int]]() 5 init(_ vec2d:[[Int]]) { 6 // perform some initialization here 7 v = vec2d 8 x = 0 9 y = 0 10 } 11 12 func next() -> Int 13 { 14 var num:Int = v[x][y] 15 y += 1 16 return num 17 } 18 19 func hasNext() -> Bool 20 { 21 while (x < v.count && y == v[x].count) { 22 x += 1 23 y = 0 24 } 25 return x < v.count 26 } 27 }