zoukankan      html  css  js  c++  java
  • 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().Show More Hint 

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

    Analyse: Use peek to invoke next first, keep the status of that as a variable. 

    Runtime: 4ms. 

     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 public:
    19     PeekingIterator(const vector<int>& nums) : Iterator(nums) {
    20         // Initialize any member here.
    21         // **DO NOT** save a copy of nums and manipulate it directly.
    22         // You should only use the Iterator interface methods.
    23     }
    24 
    25     // Returns the next element in the iteration without advancing the iterator.
    26     int peek() {
    27         if(pop) first = next();
    28         pop = false;
    29         return first;
    30     }
    31 
    32     // hasNext() and next() should behave the same as in the Iterator interface.
    33     // Override them if needed.
    34     int next() {
    35         int result = pop ? Iterator :: next() : first;
    36         pop = true;
    37         return result;
    38     }
    39 
    40     bool hasNext() const {
    41         return pop ? Iterator :: hasNext() : true;
    42     }
    43 private:
    44     int first;
    45     bool pop = true;
    46 };
  • 相关阅读:
    SignalR2结合ujtopo实现拓扑图动态变化
    SignalR2简易数据看板演示
    使用SignalR 2进行服务器广播
    使用SignalR实时Web应用程序
    ZooKeeper安装
    MongoDB安装
    线程安全与非线程安全
    监听器,事件对象,事件源
    Graphics与Canvas
    JDialog
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/5771957.html
Copyright © 2011-2022 走看看