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();
        }
    }
  • 相关阅读:
    vscode添加python文件头模板
    解决Win平台VSCode中Python在控制台输出中文乱码的问题
    Windows系统解决VSCode终端无法输入问题
    python中pip安装包出现Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None))…………或无法pip install packname安装依赖包
    GET和POST的本质区别
    文件名排序
    乱码加密解密
    jQuery.validator 自定义验证消息
    Oracle查询某个表被那些存储过程引用
    note4
  • 原文地址:https://www.cnblogs.com/airwindow/p/4805995.html
Copyright © 2011-2022 走看看