博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Peeking Iterator
阅读量:5742 次
发布时间:2019-06-18

本文共 2453 字,大约阅读时间需要 8 分钟。

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:

  1. Think of "looking ahead". You want to cache the next element.
  2. Is one variable sufficient? Why or why not?
  3. Test your design with call order of peek() before next() vs next() before peek().
  4. For a clean implementation, check out .

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

 

1 // Below is the interface for Iterator, which is already defined for you. 2 // **DO NOT** modify the interface for Iterator. 3 class Iterator { 4     struct Data; 5     Data* data; 6 public: 7     Iterator(const vector
& nums); 8 Iterator(const Iterator& iter); 9 virtual ~Iterator();10 // Returns the next element in the iteration.11 int next();12 // Returns true if the iteration has more elements.13 bool hasNext() const;14 };15 16 17 class PeekingIterator : public Iterator {18 private:19 bool hasPeeked;20 int peekElement;21 public:22 PeekingIterator(const vector
& nums) : Iterator(nums) {23 // Initialize any member here.24 // **DO NOT** save a copy of nums and manipulate it directly.25 // You should only use the Iterator interface methods.26 hasPeeked = false;27 }28 29 // Returns the next element in the iteration without advancing the iterator.30 int peek() {31 if (!hasPeeked) {32 hasPeeked = true;33 peekElement = Iterator::next();34 }35 return peekElement;36 }37 38 // hasNext() and next() should behave the same as in the Iterator interface.39 // Override them if needed.40 int next() {41 if (!hasPeeked) {42 return Iterator::next();43 }44 hasPeeked = false;45 return peekElement;46 }47 48 bool hasNext() const {49 return hasPeeked || Iterator::hasNext();50 }51 };

 

转载地址:http://xzizx.baihongyu.com/

你可能感兴趣的文章
Silverlight 2.5D RPG游戏“.NET技术”技巧与特效处理:(十二)魔法系统
查看>>
PHP 命令行模式实战之cli+mysql 模拟队列批量发送邮件(在Linux环境下PHP 异步执行脚本发送事件通知消息实际案例)...
查看>>
pyjamas build AJAX apps in Python (like Google did for Java)
查看>>
LAMP环境搭建1-mysql5.5
查看>>
centos5.9使用RPM包搭建lamp平台
查看>>
Javascript String类的属性及方法
查看>>
[LeetCode] Merge Intervals
查看>>
Ubuntu 14.04 vsftp refusing to run with writable root inside chroot问题解决方法
查看>>
Intellij IDEA远程调试tomcat
查看>>
Struts2 学习小结
查看>>
烂泥:wordpress迁移到docker
查看>>
测试工具综合
查看>>
asp.net中调用COM组件发布IIS时常见错误 80070005解决方案
查看>>
分享一段ios数据库代码,包括对表的创建、升级、增删查改
查看>>
如何书写高质量的jQuery代码
查看>>
Activity的生命周期整理
查看>>
【记录】JS toUpperCase toLowerCase 大写字母/小写字母转换
查看>>
在 Linux 系统中安装Load Generator ,并在windows 调用
查看>>
Visifire charts ToolBar
查看>>
Mysql查询
查看>>