[Java]LeetCode284. 頂端迭代器 | Peeking Iterator

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-plgmfxkv-kx.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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().java

Example:git

Assume that the iterator is initialized to the beginning of the list: .

Call  gets you 1, the first element in the list.
Now you call  and it returns 2, the next element. Calling  after that still return 2. 
You call  the final time and it returns 3, the last element. 
Calling  after that should return false.
[1,2,3]next()peek()next()next()hasNext()

Follow up: How would you extend your design to be generic and work with all types, not just integer?github


給定一個迭代器類的接口,接口包含兩個方法: next() 和 hasNext()。設計並實現一個支持 peek() 操做的頂端迭代器 -- 其本質就是把本來應由 next() 方法返回的元素 peek() 出來。api

示例:微信

假設迭代器被初始化爲列表 。

調用 返回 1,獲得列表中的第一個元素。
如今調用  返回 2,下一個元素。在此以後調用 仍然返回 2。
最後一次調用  返回 3,末尾元素。在此以後調用  應該返回 false。
[1,2,3]next()peek()next()next()hasNext()

進階:你將如何拓展你的設計?使之變得通用化,從而適應全部的類型,而不僅是整數型?oracle


45mside

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

46msthis

 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     
 5     private LinkedList<Integer> list = new LinkedList<Integer>();
 6 
 7     public PeekingIterator(Iterator<Integer> iterator) {
 8         while (iterator.hasNext()) {
 9             list.add(iterator.next());
10         }
11     }
12 
13     // Returns the next element in the iteration without advancing the iterator.
14     public Integer peek() {
15         if (list.isEmpty()) {
16             return null;
17         }
18         return list.getFirst();
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         return list.removeFirst();
26     }
27 
28     @Override
29     public boolean hasNext() {
30         return !list.isEmpty();
31     }
32 }

47msspa

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