Leetcode: Peeking Iterator

Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation -- it essentially peek() at the element that will be returned by the next call to next().

Here is an example. Assume that the iterator is initialized to the beginning of the list: [1, 2, 3].

Call next() gets you 1, the first element in the list.

Now you call peek() and it returns 2, the next element. Calling next() after that still return 2.

You call next() the final time and it returns 3, the last element. Calling hasNext() after that should return false.

Hint:

Think of "looking ahead". You want to cache the next element.
Is one variable sufficient? Why or why not?
Test your design with call order of peek() before next() vs next() before peek().
For a clean implementation, check out Google's guava library source code.
Follow up: How would you extend your design to be generic and work with all types, not just integer?

My thinking is: 

use a double cache: long cache, when no number is cached, cache = Long.MIN_VALUE. 

When i call next(), it will firstly check if there's any number cached. If yes, clear the cache and return that value.

When I call peek(), firstly check if there's any number cached. If yes, always return that value. If no, call iterator.next() and cache the new value.

syntax: How can I convert a long to int in Java: 

Simple type casting should do it:

long l = 100000;
int i = (int) l;

Note, however, that large numbers (usually larger than 2147483647 and smaller than -2147483648) will lose some of the bits and would be represented incorrectly.

For instance, 2147483648 would be represented as -2147483648.

我的做法:

 1 // Java Iterator interface reference:
 2 // https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
 3 class PeekingIterator implements Iterator<Integer> {
 4     long cache;
 5     Iterator<Integer> iter;
 6 
 7     public PeekingIterator(Iterator<Integer> iterator) {
 8         // initialize any member here.
 9         this.cache = Long.MIN_VALUE;
10         this.iter = iterator;
11     }
12 
13     // Returns the next element in the iteration without advancing the iterator.
14     public Integer peek() {
15         if (cache != Long.MIN_VALUE) {
16             return (int)cache;
17         }
18         else {
19             cache = (long)iter.next();
20             return (int)cache;
21         }
22     }
23 
24     // hasNext() and next() should behave the same as in the Iterator interface.
25     // Override them if needed.
26     @Override
27     public Integer next() {
28         if (cache != Long.MIN_VALUE) {
29             int temp = (int)cache;
30             cache = Long.MIN_VALUE;
31             return temp;
32         }
33         else return iter.next();
34     }
35 
36     @Override
37     public boolean hasNext() {
38         return (cache!=Long.MIN_VALUE) || (iter.hasNext());
39     }
40 }

Disscuss 版vote最高做法: next用null

 1 class PeekingIterator implements Iterator<Integer> {  
 2     private Integer next = null;
 3     private Iterator<Integer> iter;
 4 
 5     public PeekingIterator(Iterator<Integer> iterator) {
 6         // initialize any member here.
 7         iter = iterator;
 8         if (iter.hasNext())
 9             next = iter.next();
10     }
11     
12     // Returns the next element in the iteration without advancing the iterator. 
13     public Integer peek() {
14         return next; 
15     }
16 
17     // hasNext() and next() should behave the same as in the Iterator interface.
18     // Override them if needed.
19     @Override
20     public Integer next() {
21         Integer res = next;
22         next = iter.hasNext() ? iter.next() : null;
23         return res; 
24     }
25 
26     @Override
27     public boolean hasNext() {
28         return next != null;
29     }
30 }

Follow Up: https://discuss.leetcode.com/topic/75806/simple-java-solution-working-for-generic-types

Most solutions I've seen do not work for generic types as they use null as a special value to indicate the end of the iterator. However, what if we wanted to iterate within possibly null values? If we want to allow null values inside the iterator we need to add an extra flag to indicate whether we are done or not.

 1 class PeekingIterator implements Iterator<Integer> {
 2         Integer next;
 3         boolean done;
 4         Iterator<Integer> it;
 5     public PeekingIterator(Iterator<Integer> iterator) {
 6         // initialize any member here.
 7         it = iterator;
 8         if(it.hasNext()) { 
 9             next = it.next();
10             done = false;
11         }else{
12             done = true;
13         }
14     }
15 
16     // Returns the next element in the iteration without advancing the iterator.
17     public Integer peek() {
18         return next;
19     }
20 
21     // hasNext() and next() should behave the same as in the Iterator interface.
22     // Override them if needed.
23     @Override
24     public Integer next() {
25         Integer res = next;
26         if(it.hasNext()) { 
27             next = it.next();
28         }else{
29             done = true;
30         }
31         return res;
32     }
33 
34     @Override
35     public boolean hasNext() {
36         return !done;
37     }
38 }
原文地址:https://www.cnblogs.com/EdwardLiu/p/5077593.html