[Leetcode] Peeking Iterator, Solution
2015-10-03 12:43
477 查看
Given an Iterator class interface with methods:
[code]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<int>& 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: class PeekingIterator : public Iterator { 16: public: 17: PeekingIterator(const vector<int>& nums) : Iterator(nums) { 18: // Initialize any member here. 19: // **DO NOT** save a copy of nums and manipulate it directly. 20: // You should only use the Iterator interface methods. 21: this->length = nums.size(); 22: this->nums = &nums; 23: } 24: // Returns the next element in the iteration without advancing the iterator. 25: int peek() { 26: if(currentIndex < length) { 27: return (*this->nums)[currentIndex]; 28: } 29: return -1; 30: } 31: // hasNext() and next() should behave the same as in the Iterator interface. 32: // Override them if needed. 33: int next() { 34: if(currentIndex < length) { 35: currentIndex ++; 36: return (*this->nums)[currentIndex-1]; 37: } 38: return -1; 39: } 40: bool hasNext() const { 41: return currentIndex < length; 42: } 43: private: 44: int currentIndex = 0; 45: int length = 0; 46: const vector<int>* nums = NULL; 47: };github:[/b]https://github.com/codingtmd/leetcode/blob/master/src/Peeking_Iterator.cpp
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[/i] return 2.You call
next()the final time and it returns 3, the last element. Calling
hasNext()after that should return false.[Thoughts]链表的一道设计题。next其实在这里就是获取头元素的值,并移动到下一个元素。peek在这里就是返回当前元素的值。
[code]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<int>& 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: class PeekingIterator : public Iterator { 16: public: 17: PeekingIterator(const vector<int>& nums) : Iterator(nums) { 18: // Initialize any member here. 19: // **DO NOT** save a copy of nums and manipulate it directly. 20: // You should only use the Iterator interface methods. 21: this->length = nums.size(); 22: this->nums = &nums; 23: } 24: // Returns the next element in the iteration without advancing the iterator. 25: int peek() { 26: if(currentIndex < length) { 27: return (*this->nums)[currentIndex]; 28: } 29: return -1; 30: } 31: // hasNext() and next() should behave the same as in the Iterator interface. 32: // Override them if needed. 33: int next() { 34: if(currentIndex < length) { 35: currentIndex ++; 36: return (*this->nums)[currentIndex-1]; 37: } 38: return -1; 39: } 40: bool hasNext() const { 41: return currentIndex < length; 42: } 43: private: 44: int currentIndex = 0; 45: int length = 0; 46: const vector<int>* nums = NULL; 47: };github:[/b]https://github.com/codingtmd/leetcode/blob/master/src/Peeking_Iterator.cpp
相关文章推荐
- QXmlStreamReader 读取xml 文件
- 【转】hadoop机架感知
- Android Studio 安装新 code themes
- ISO/IEC 9899:2011 条款6.5.5——乘法操作符
- UVA - 232 Crossword Answers
- 平方和立方和公式的推导及其拓展
- JS(ajax笔记)
- anni
- 【HTML+CSS】(1)基本语法
- Ubuntu用Wine运行Windows应用程序和卸载
- Picasso VS Glide
- 基于API的前后端分离开发
- hdu 2846 Repository
- HBase体系结构(转)
- 工厂模式与抽象工厂模式
- RGB转jpg使用libjpeg库实现
- IOS9自定义UIWindow被键盘盖住(在键盘下面)解决方法
- tyvj|P1089 smrtfun
- javaWeb开发之基础--------------MySQL数据库总结
- LintCode "Coins in a Line"