zoukankan      html  css  js  c++  java
  • [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 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?

    Analysis:

    This kind of problem is very easy! But you need try hold some good pinciple in design and implementation, it could greatly improve your coding ability. 
    
    When you meet the problem of implementing a specific iterator, the first thing is not try to come up with your own totaly innovative iterator.
    Like: list.get(i) (and control over i)
    Even that way is possible, but you have to tackle may possible cases, which is hard to be right!!!
    
    However, you should take advantage of existing iterator of arguments!!! Which have already implemented a delicate iterator, with common actions: hasNext(), next(). 
    
    Define your invariant:
    For this problem, we want our "cur_iterator" always point to right position, thus we could directly use underlying "iterator1.hasNext()" or "iterator2.hasNext()". And once we make sure next element is existed, we could use "cur_iterator.next()" to get the right answer back. 
    Since we need to have a special overall iterator for the problem, and this iterator actually takes advantage of other iterators together, our goal is to find the right mechanims to "use underlying iterators to acheive the effect that the overall iterator displays". 
    
    Step 1: Initialize overall iterator. 
    this.cur_iterator = (this.iterator1.hasNext() ? this.iterator1 : this.iterator2);
    Note: iff there are unscanned elements in lists, we must guarantee next() function could reach them. 
    <You must consider the case of l1: [], l2: [1, 2, 3]> And the luckily thing is we don't need to test l1's length, directly use l1.hasNext() could elegantly achieve this purpose.
    
    Step 2: Adjust the iterator when we finish the scan of an element.
    The core part for "ZigZag operation".
    Key: point to the other iterator when other iterator still has unscanned elements. Otherwise, remain on the same list.
    if (cur_iterator == iterator1) {
        if (iterator2.hasNext()) {
            cur_iterator = iterator2;
        }
    }

    Solution:

    public class ZigzagIterator {
        Iterator<Integer> cur_iterator;
        Iterator<Integer> iterator1;
        Iterator<Integer> iterator2;
        
        public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
            this.iterator1 = v1.iterator();
            this.iterator2 = v2.iterator();
            this.cur_iterator = (this.iterator1.hasNext() ? this.iterator1 : this.iterator2);
        }
    
        public int next() {
            int ret = cur_iterator.next();
            if (cur_iterator == iterator1) {
                if (iterator2.hasNext()) {
                    cur_iterator = iterator2;
                }
            } else{
                if (iterator1.hasNext()) {
                    cur_iterator = iterator1;
                }
            }
            return ret;
        }
    
        public boolean hasNext() {
            return cur_iterator.hasNext();
        }
    }
  • 相关阅读:
    怎么使用 Jupyter Notebook 进入指定的目录
    安卓手机安装Xposed框架/钉钉虚拟定位
    Some of the Example google dorks 2019谷歌hacker语法检索表
    在心脏内部,普通的WEB用户会怎样? 心脏滴血漏洞分析学习
    Windows的安全配置基础,打造一个安全点的Windows
    计算机存储单位
    Python如何安装whl文件,python安装py包
    jdk文件中javaw的作用
    msfconsole基础使用,简洁高效学习版
    VirtualBox报错:不能为虚拟电脑XXX打开一个新任务
  • 原文地址:https://www.cnblogs.com/airwindow/p/4805995.html
Copyright © 2011-2022 走看看