281. Zigzag Iterator - Medium

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

Example:

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

Output: [1,3,2,4,5,6]

Explanation: 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:
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:

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

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

用一个queue来存每个list的iterator。call next()时,从q中poll出先进queue的iterator,返回其next值,如果它没到末尾,还要把它存回queue以备下次使用。call hasNext()就是判断queue是否为空。

时间:O(N),空间:O(1)

public class ZigzagIterator {
    Queue<Iterator> q;

    public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
        q = new LinkedList<>();
        if(!v1.isEmpty())
            q.offer(v1.iterator());
        if(!v2.isEmpty())
            q.offer(v2.iterator());
    }

    public int next() {
        Iterator iter = q.poll();
        int val = (int)iter.next();
        if(iter.hasNext())
            q.offer(iter);
        return val;
    }

    public boolean hasNext() {
        return !q.isEmpty();
    }
}

/**
 * Your ZigzagIterator object will be instantiated and called as such:
 * ZigzagIterator i = new ZigzagIterator(v1, v2);
 * while (i.hasNext()) v[f()] = i.next();
 */

原文地址:https://www.cnblogs.com/fatttcat/p/10037506.html

时间: 2024-10-30 07:43:31

281. Zigzag Iterator - Medium的相关文章

281. Zigzag Iterator

题目: 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 shou

[LeetCode#281] Zigzag Iterator

Problem: 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

281. Zigzag Iterator z字型遍历

[抄题]: Given two 1d vectors, implement an iterator to return their elements alternately. Example: Input: v1 = [1,2] v2 = [3,4,5,6] Output: [1,3,2,4,5,6] Explanation: By calling next repeatedly until hasNext returns false,   the order of elements retur

Leetcode: Zigzag Iterator

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 b

Zigzag Iterator

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 b

[LeetCode] 6. ZigZag Conversion (Medium)

原题链接 把字符串按照 ↓↓--的顺序,排列成一个 Z 形,返回 从左到右,按行读得的字符串. 思路: 建立一个二维数组来按行保存字符串. 按照 ↓↓--的方向进行对每一行加入字符. 太慢了这个解法,Runtime: 96 ms, faster than 3.61% of C++. class Solution { public: string convert(string s, int numRows) { if (numRows <= 1) return s; string res; str

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

20200108-20200112

244. Shortest Word Distance II - Medium 245. Shortest Word Distance III - Medium 246. Strobogrammatic Number - Easy 247. Strobogrammatic Number II - Medium method: recursion n =1 [0,1,8] n = 2 [11, 88, 69, 96] n = 3 [101, 111, 181, 808, 818, 888, 609

leetcode 锁掉的题目清单

也刷leetcode, 先把锁掉的题目留备份好了: 156 Binary Tree Upside Down  [1] Problem: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tre