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 };
  • 相关阅读:
    java多线程详解(7)-线程池的使用
    mysql学习(4)-mysqldump备份和恢复数据
    mysql学习(3)-linux下mysql主从复制
    java多线程详解(5)-Threadlocal用法
    java多线程详解(4)-多线程同步技术与lock
    java多线程详解(3)-线程的互斥与同步
    iava多线程详解(2)-成员变量与局部变量访问
    java多线程详解(1)-多线程入门
    redis 的消息发布订阅
    redis 数据类型
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/5771957.html
Copyright © 2011-2022 走看看