zoukankan      html  css  js  c++  java
  • [LeetCode] Peeking Iterator

    Peeking Iterator

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

     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 
    16 
    17 class PeekingIterator : public Iterator {
    18 private:
    19     bool hasPeeked;
    20     int peekElement;
    21 public:
    22     PeekingIterator(const vector<int>& 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 };
  • 相关阅读:
    终于有人把MYSQL索引讲清楚了
    计算机基础知识总结与操作系统 PDF 下载
    java电子书python电子书等PDF下载方式
    redis布隆过滤器
    IntelliJ IDEA 2020.1 激活教程,亲测可用
    Another Redis DeskTop Manager一款稳定全新的redis连接工具
    一文吃透redis持久化,妈妈再也不担心我面试过不了!
    ES6 运算符
    If-Else的5种方法从入门到高级示例
    ES6中 的类(class)
  • 原文地址:https://www.cnblogs.com/easonliu/p/4826936.html
Copyright © 2011-2022 走看看