[leetcode#281] Zigzag Iterator

Source: Internet
Author: User

problem:

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

For example, given 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 being extended to such cases?

Analysis:

This kind of problem is very easy! But you needTryHold 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 are notTryTo come up with your own totaly innovative iterator. Like:list.get (i) (and control over I) even that's the possible, but you had to tackle may possible cases, which was hard to is right!!!However, should take advantage of existing iterator of arguments!!!which has already implemented a delicate iterator, with common Actions:hasnext (), Next (). Define your invariant:for ThisProblem, we want our "cur_iterator" always point-to-right position, thus we could directly use underlying "Iterator1.hasne XT () "or" Iterator2.hasnext () ". And once we make sure next element are existed, we could use "cur_iterator.next ()"To get the right answer back. Since we need to has a special overall iterator forThe problem, and ThisIterator actually takes advantage of other iterators together, our goal are to find the right mechanims to ' use underlying Iterators to acheive the effect, the overall iterator displays ". Step1: Initialize overall iterator. This. Cur_iterator = ( This. Iterator1.hasnext ()? This. Iterator1: This. Iterator2); Note:iff there is unscanned elements in lists, we must guarantee next () function could reach them. <you must consider the Caseof L1: [], L2: [1, 2, 3]> and the luckily thing are we don ' t need to test L1 's length, directly use L1.hasnext () could E legantly Achieve Thispurpose. Step2: Adjust The iterator when we finish the scan of an element. The core part for"ZigZag operation". Key:point to the other iterator if other iterator still have unscanned elements. Otherwise, remain on the same list.if(Cur_iterator = =Iterator1) {    if(Iterator2.hasnext ()) {Cur_iterator=Iterator2; }}

Solution:

 Public classZigzagiterator {Iterator<Integer>Cur_iterator; Iterator<Integer>Iterator1; Iterator<Integer>Iterator2;  PublicZigzagiterator (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 intNext () {intRET =Cur_iterator.next (); if(Cur_iterator = =Iterator1) {            if(Iterator2.hasnext ()) {Cur_iterator=Iterator2; }        } Else{            if(Iterator1.hasnext ()) {Cur_iterator=Iterator1; }        }        returnret; }     Public BooleanHasnext () {returnCur_iterator.hasnext (); }}

[leetcode#281] Zigzag Iterator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.