首页 > 其他 > 详细

Leetcode: Peeking Iterator

时间:2015-12-26 09:58:11      阅读:226      评论:0      收藏:0      [点我收藏+]
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 }

 

Follow Up: 参考http://segmentfault.com/a/1190000003794961

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

 

Leetcode: Peeking Iterator

原文:http://www.cnblogs.com/EdwardLiu/p/5077593.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!