日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[Swift]LeetCode281. 之字形迭代器 $ Zigzag Iterator

發(fā)布時(shí)間:2023/12/1 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Swift]LeetCode281. 之字形迭代器 $ Zigzag Iterator 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公眾號(hào):山青詠芝(shanqingyongzhi)
?博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
?GitHub地址:https://github.com/strengthen/LeetCode
?原文地址:?https://www.cnblogs.com/strengthen/p/10686001.html?
?如果鏈接不是山青詠芝的博客園地址,則可能是爬取作者的文章。
?原文已修改更新!強(qiáng)烈建議點(diǎn)擊原文地址閱讀!支持作者!支持原創(chuàng)!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

Given two 1d vectors, implement an iterator to return their elements alternately.

For example, given two 1d vectors:

v1 = [1, 2] v2 = [3, 4, 5, 6]

By calling?next?repeatedly until?hasNext?returns?false, the order of elements returned by?next?should be:?[1, 3, 2, 4, 5, 6].

Follow up: What if you are given?k?1d vectors? How well can your code be extended to such cases?

Clarification for the follow up question -?Update (2015-09-18):
The "Zigzag" order is not clearly defined and is ambiguous for?k > 2?cases. If "Zigzag" does not look right to you, replace "Zigzag" with "Cyclic". For example, given the following input:

[1,2,3] [4,5,6,7] [8,9]

It should return?[1,4,8,2,5,9,3,6,7].


給定兩個(gè)一維向量,實(shí)現(xiàn)迭代器交替返回元素。

例如,給定兩個(gè)一維向量:

v1 = [1, 2] v2 = [3, 4, 5, 6]

通過反復(fù)調(diào)用next直到hasNext返回false,next返回的元素順序應(yīng)該是:[1,3,2,4,5,6]。

后續(xù):如果你得到k 1d向量怎么辦?您的代碼在這種情況下可以擴(kuò)展到什么程度?

后續(xù)問題澄清-更新(2015-09-18):

“之字形”順序沒有明確定義,對(duì)于k>2的情況不明確。如果“之字形”看起來不正確,請(qǐng)用“循環(huán)”替換“之字形”。例如,給定以下輸入:

[1,2,3] [4,5,6,7] [8,9]

它應(yīng)該返回[1,4,8,2,5,9,3,6,7]。


Solution:

1 class ZigzagIterator { 2 var v:[Int] = [Int]() 3 var i:Int = 0 4 init(_ v1:[Int],_ v2:[Int]) 5 { 6 var n1:Int = v1.count 7 var n2:Int = v2.count 8 let n:Int = max(n1, n2) 9 for i in 0..<n 10 { 11 if i < n1 {v.append(v1[i])} 12 if i < n2 {v.append(v2[i])} 13 } 14 } 15 16 func next() -> Int 17 { 18 let num:Int = v[i] 19 i += 1 20 return num 21 } 22 23 func hasNext() -> Bool 24 { 25 return i < v.count 26 } 27 }

點(diǎn)擊:Playground測(cè)試

1 var zigzag = ZigzagIterator([1, 2],[3, 4, 5, 6]) 2 while(zigzag.hasNext()) 3 { 4 print(zigzag.next()) 5 } 6 //Print 7 /* 8 1 9 3 10 2 11 4 12 5 13 6 14 */

?

轉(zhuǎn)載于:https://www.cnblogs.com/strengthen/p/10686001.html

總結(jié)

以上是生活随笔為你收集整理的[Swift]LeetCode281. 之字形迭代器 $ Zigzag Iterator的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。