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();
        }
    }
  • 相关阅读:
    改变传统电视对客厅文化的影响
    移动终端三分天下 何与争峰
    全球移动IM应用的迅猛发展前景
    电子医疗的发展和实现
    poj 1523(无向图求割点)
    poj 3255(次短路)
    poj 2125
    poj 3204
    图的连通度问题的求法(转)
    poj 3308
  • 原文地址:https://www.cnblogs.com/airwindow/p/4805995.html
Copyright © 2011-2022 走看看