敢教日月换新天。为有牺牲多壮志,

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

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10688866.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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

Example:

[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?


给定一个迭代器类的接口,接口包含两个方法: next() 和 hasNext()。设计并实现一个支持 peek() 操作的顶端迭代器 -- 其本质就是把原本应由 next() 方法返回的元素 peek() 出来。

示例:

[1,2,3]
next() 
peek()
next() 
next()
hasNext()

进阶:你将如何拓展你的设计?使之变得通用化,从而适应所有的类型,而不只是整数型?


45ms

复制代码
 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 }
复制代码

46ms

复制代码
 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 }
复制代码

47ms

复制代码
 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 }
复制代码

 

posted @   为敢技术  阅读(295)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°