Leetcode - Zigzag Iterator

Leetcode - Zigzag Iterator_第1张图片
Screenshot from 2016-02-17 22:03:02.png

My code:

public class ZigzagIterator {
    private int i1 = 0;
    private int i2 = 0;
    private boolean first = true;
    private List l1;
    private List l2;
    public ZigzagIterator(List v1, List v2) {
        this.l1 = v1;
        this.l2 = v2;
    }

    public int next() {
        if (first) {
            if (i1 < l1.size()) {
                first = false;
                return l1.get(i1++);
            }
            else {
                return l2.get(i2++);
            }
        }
        else {
            if (i2 < l2.size()) {
                first = true;
                return l2.get(i2++);
            }
            else {
                return l1.get(i1++);
            }
        }
    }

    public boolean hasNext() {
        if (i1 >= l1.size() && i2 >= l2.size())
            return false;
        else
            return true;
    }
}

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

这个题目没什么意思。
要么改进难度。如果是 k 个list呢?

我自己写了下,并且测试了下,应该基本没问题。

My code:

public class Solution{ 
     private List> l;
     private int counter = 0; // index of list
     private int[] index; // index of element in every list
     private int k;
     public Solution(List> v) {
         this.l = v;
         k = v.size();
         index = new int[k];
     }

        public int next() {
            int lNum = counter % k;
            if (index[lNum] < l.get(lNum).size()) {
                counter++;
                index[lNum]++;
                return l.get(lNum).get(index[lNum] - 1);
            }
            counter++;
            while (counter % k != lNum) {
                if (index[counter % k] < l.get(counter % k).size()) {
                    index[counter % k]++;
                    counter++;
                    return l.get((counter - 1) % k).get(index[(counter - 1) % k] - 1);
                }
                else
                    counter++;
            }
            return -1;
        }

        public boolean hasNext() {
            for (int i = 0; i < k; i++) {
                if (index[i] < l.get(i).size())
                    return true;
            }
            return false;
        }   
    
    public static void main(String[] args) {
        ArrayList l1 = new ArrayList();
        l1.add(1);
        l1.add(2);
        l1.add(3);
        
        ArrayList l2 = new ArrayList();
        l2.add(4);
        l2.add(5);
        l2.add(6);
        l2.add(7);
        
        ArrayList l3 = new ArrayList();
        l3.add(8);
        
        ArrayList> l = new ArrayList>();
        l.add(l1);
        l.add(l2);
        l.add(l3);
        
        Solution test = new Solution(l);
        while (test.hasNext()) {
            System.out.println(test.next());
        }
    }
 }  

就是使用一个counter进行轮询, round-robin,找到某个list还没有完全遍历完。下次从他下一个开始轮询。

Anyway, Good luck, Richardo!

这道题目并不难。自己写了下。

My code:

public class ZigzagIterator {
    List> iters = new ArrayList>();
    int counter = 0;
    private int total = 2;
    public ZigzagIterator(List v1, List v2) {
        iters.add(v1.iterator());
        iters.add(v2.iterator());
    }

    public int next() {
        for (int i = 0; i < total; i++) {
            if (iters.get((counter + i) % total).hasNext()) {
                int ret = iters.get((counter + i) % total).next();
                counter++;
                return ret;
            }
        }
        return -1;
    }

    public boolean hasNext() {
        for (int i = 0; i < total; i++) {
            if (iters.get((counter + i) % total).hasNext()) {
                return true;
            }
        }
        
        return false;
    }
}

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

我是以,k个list,做zigzag iterator的角度来做这道题目的。

然后之前做过一道题目,叫做
Flatten Nested List Iterator

做完那道题目,这个题目的思路就很清晰了。

的确,遍历的时候要学会多用Java自带的 Iterator,就对是神器。

Anyway, Good luck, Richardo! -- 09/11/2016

你可能感兴趣的:(Leetcode - Zigzag Iterator)