返回介绍

solution / 0200-0299 / 0284.Peeking Iterator / README

发布于 2024-06-17 01:04:02 字数 7196 浏览 0 评论 0 收藏 0

284. 窥视迭代器

English Version

题目描述

请你在设计一个迭代器,在集成现有迭代器拥有的 hasNextnext 操作的基础上,还额外支持 peek 操作。

实现 PeekingIterator 类:

  • PeekingIterator(Iterator<int> nums) 使用指定整数迭代器 nums 初始化迭代器。
  • int next() 返回数组中的下一个元素,并将指针移动到下个元素处。
  • bool hasNext() 如果数组中存在下一个元素,返回 true ;否则,返回 false
  • int peek() 返回数组中的下一个元素,但 移动指针。

注意:每种语言可能有不同的构造函数和迭代器 Iterator,但均支持 int next()boolean hasNext() 函数。

 

示例 1:

输入:
["PeekingIterator", "next", "peek", "next", "next", "hasNext"]
[[[1, 2, 3]], [], [], [], [], []]
输出:
[null, 1, 2, 2, 3, false]

解释:
PeekingIterator peekingIterator = new PeekingIterator([1, 2, 3]); // [1,2,3]
peekingIterator.next();  // 返回 1 ,指针移动到下一个元素 [1,2,3]
peekingIterator.peek();  // 返回 2 ,指针未发生移动 [1,2,3]
peekingIterator.next();  // 返回 2 ,指针移动到下一个元素 [1,2,3]
peekingIterator.next();  // 返回 3 ,指针移动到下一个元素 [1,2,3]
peekingIterator.hasNext(); // 返回 False

 

提示:

  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 1000
  • nextpeek 的调用均有效
  • nexthasNextpeek最多调用  1000

 

进阶:你将如何拓展你的设计?使之变得通用化,从而适应所有的类型,而不只是整数型?

解法

方法一

# Below is the interface for Iterator, which is already defined for you.
#
# class Iterator:
#   def __init__(self, nums):
#     """
#     Initializes an iterator object to the beginning of a list.
#     :type nums: List[int]
#     """
#
#   def hasNext(self):
#     """
#     Returns true if the iteration has more elements.
#     :rtype: bool
#     """
#
#   def next(self):
#     """
#     Returns the next element in the iteration.
#     :rtype: int
#     """


class PeekingIterator:
  def __init__(self, iterator):
    """
    Initialize your data structure here.
    :type iterator: Iterator
    """
    self.iterator = iterator
    self.has_peeked = False
    self.peeked_element = None

  def peek(self):
    """
    Returns the next element in the iteration without advancing the iterator.
    :rtype: int
    """
    if not self.has_peeked:
      self.peeked_element = self.iterator.next()
      self.has_peeked = True
    return self.peeked_element

  def next(self):
    """
    :rtype: int
    """
    if not self.has_peeked:
      return self.iterator.next()
    result = self.peeked_element
    self.has_peeked = False
    self.peeked_element = None
    return result

  def hasNext(self):
    """
    :rtype: bool
    """
    return self.has_peeked or self.iterator.hasNext()


# Your PeekingIterator object will be instantiated and called as such:
# iter = PeekingIterator(Iterator(nums))
# while iter.hasNext():
#   val = iter.peek()   # Get the next element but not advance the iterator.
#   iter.next()     # Should return the same value as [val].
// Java Iterator interface reference:
// https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html

class PeekingIterator implements Iterator<Integer> {
  private Iterator<Integer> iterator;
  private boolean hasPeeked;
  private Integer peekedElement;

  public PeekingIterator(Iterator<Integer> iterator) {
    // initialize any member here.
    this.iterator = iterator;
  }

  // Returns the next element in the iteration without advancing the iterator.
  public Integer peek() {
    if (!hasPeeked) {
      peekedElement = iterator.next();
      hasPeeked = true;
    }
    return peekedElement;
  }

  // hasNext() and next() should behave the same as in the Iterator interface.
  // Override them if needed.
  @Override
  public Integer next() {
    if (!hasPeeked) {
      return iterator.next();
    }
    Integer result = peekedElement;
    hasPeeked = false;
    peekedElement = null;
    return result;
  }

  @Override
  public boolean hasNext() {
    return hasPeeked || iterator.hasNext();
  }
}
/*
 * Below is the interface for Iterator, which is already defined for you.
 * **DO NOT** modify the interface for Iterator.
 *
 *  class Iterator {
 *    struct Data;
 *     Data* data;
 *  public:
 *    Iterator(const vector<int>& nums);
 *     Iterator(const Iterator& iter);
 *
 *     // Returns the next element in the iteration.
 *    int next();
 *
 *    // Returns true if the iteration has more elements.
 *    bool hasNext() const;
 *  };
 */

class PeekingIterator : public Iterator {
public:
  PeekingIterator(const vector<int>& nums)
    : Iterator(nums) {
    // Initialize any member here.
    // **DO NOT** save a copy of nums and manipulate it directly.
    // You should only use the Iterator interface methods.
    hasPeeked = false;
  }

  // Returns the next element in the iteration without advancing the iterator.
  int peek() {
    if (!hasPeeked) {
      peekedElement = Iterator::next();
      hasPeeked = true;
    }
    return peekedElement;
  }

  // hasNext() and next() should behave the same as in the Iterator interface.
  // Override them if needed.
  int next() {
    if (!hasPeeked) return Iterator::next();
    hasPeeked = false;
    return peekedElement;
  }

  bool hasNext() const {
    return hasPeeked || Iterator::hasNext();
  }

private:
  bool hasPeeked;
  int peekedElement;
};
/*   Below is the interface for Iterator, which is already defined for you.
 *
 *   type Iterator struct {
 *
 *   }
 *
 *   func (this *Iterator) hasNext() bool {
 *    // Returns true if the iteration has more elements.
 *   }
 *
 *   func (this *Iterator) next() int {
 *    // Returns the next element in the iteration.
 *   }
 */

type PeekingIterator struct {
  iter      *Iterator
  hasPeeked   bool
  peekedElement int
}

func Constructor(iter *Iterator) *PeekingIterator {
  return &PeekingIterator{iter, iter.hasNext(), iter.next()}
}

func (this *PeekingIterator) hasNext() bool {
  return this.hasPeeked || this.iter.hasNext()
}

func (this *PeekingIterator) next() int {
  if !this.hasPeeked {
    return this.iter.next()
  }
  this.hasPeeked = false
  return this.peekedElement
}

func (this *PeekingIterator) peek() int {
  if !this.hasPeeked {
    this.peekedElement = this.iter.next()
    this.hasPeeked = true
  }
  return this.peekedElement
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文