美文网首页
281. Zigzag Iterator

281. Zigzag Iterator

作者: 我是你的果果呀 | 来源:发表于2016-12-07 08:14 被阅读0次

    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 callingnextrepeatedly untilhasNextreturnsfalse, the order of elements returned bynextshould be:[1, 3, 2, 4, 5, 6].
    Follow up: What if you are givenk1d vectors? How well can your code be extended to such cases?

    setfan  大神的code!!! ,膜拜, 用个临时变量 swap iter1 和2. 我用了一个flag 变量检查是否应该从iter1 开始, 关于follow up 问题, 有一个大神用了linkedList, 拿出来, 放进去的淘腾, 其实用queue 更直观, idea 有了, 代码不难, 就不贴了。

    相关文章

      网友评论

          本文标题:281. Zigzag Iterator

          本文链接:https://www.haomeiwen.com/subject/poldmttx.html