leetcode - 284. Peeking Iterator

Description

Design an iterator that supports the peek operation on an existing iterator in addition to the hasNext and the next operations.

Implement the PeekingIterator class:

  • PeekingIterator(Iterator nums) Initializes the object with the given integer iterator iterator.
  • int next() Returns the next element in the array and moves the pointer to the next element.
  • boolean hasNext() Returns true if there are still elements in the array.
  • int peek() Returns the next element in the array without moving the pointer.

Note: Each language may have a different implementation of the constructor and Iterator, but they all support the int next() and boolean hasNext() functions.

Example 1:

Input
["PeekingIterator", "next", "peek", "next", "next", "hasNext"]
[[[1, 2, 3]], [], [], [], [], []]
Output
[null, 1, 2, 2, 3, false]

Explanation
PeekingIterator peekingIterator = new PeekingIterator([1, 2, 3]); // [1,2,3]
peekingIterator.next();    // return 1, the pointer moves to the next element [1,2,3].
peekingIterator.peek();    // return 2, the pointer does not move [1,2,3].
peekingIterator.next();    // return 2, the pointer moves to the next element [1,2,3]
peekingIterator.next();    // return 3, the pointer moves to the next element [1,2,3]
peekingIterator.hasNext(); // return False

Constraints:

1 <= nums.length <= 1000
1 <= nums[i] <= 1000
All the calls to next and peek are valid.
At most 1000 calls will be made to next, hasNext, and peek.

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

Solution

Use a cache to store the next element.

Code

# 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.next_one = None
        if self.iterator.hasNext():
            self.next_one = self.iterator.next()

    def peek(self):
        """
        Returns the next element in the iteration without advancing the iterator.
        :rtype: int
        """
        return self.next_one
        

    def next(self):
        """
        :rtype: int
        """
        old_next = self.next_one
        if self.iterator.hasNext():
            self.next_one = self.iterator.next()
        else:
            self.next_one = None
        return old_next


    def hasNext(self):
        """
        :rtype: bool
        """
        return self.next_one is not None

# 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].

相关推荐

  1. leetcode - 284. Peeking Iterator

    2024-03-25 11:02:05       39 阅读
  2. Leetcode274

    2024-03-25 11:02:05       31 阅读
  3. leetcode 24

    2024-03-25 11:02:05       47 阅读
  4. LeetCode 2884. 修改列

    2024-03-25 11:02:05       61 阅读
  5. LeetCode 224:基本计算器

    2024-03-25 11:02:05       59 阅读
  6. leetcode283】移动零

    2024-03-25 11:02:05       56 阅读
  7. [leetcode] 283. 移动零

    2024-03-25 11:02:05       45 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-25 11:02:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-25 11:02:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-25 11:02:05       82 阅读
  4. Python语言-面向对象

    2024-03-25 11:02:05       91 阅读

热门阅读

  1. 天猫开店怎么发布产品

    2024-03-25 11:02:05       43 阅读
  2. 蓝桥杯刷题_day3

    2024-03-25 11:02:05       40 阅读
  3. vue v-for指令

    2024-03-25 11:02:05       36 阅读
  4. linux系统Kubernetes工具ingress暴露服务

    2024-03-25 11:02:05       36 阅读
  5. video/pdf文件预览与进度上传

    2024-03-25 11:02:05       41 阅读
  6. 代码审计与web安全-第四章作业

    2024-03-25 11:02:05       40 阅读
  7. vue3.0-monaco组件封装

    2024-03-25 11:02:05       36 阅读
  8. 1. 一起学习机器学习 -- Data_exploration

    2024-03-25 11:02:05       36 阅读
  9. QT GUI常用函数介绍

    2024-03-25 11:02:05       41 阅读
  10. React-创建虚拟Dom四种方法

    2024-03-25 11:02:05       36 阅读
  11. 网络安全实训Day12

    2024-03-25 11:02:05       37 阅读
  12. 图像去噪与增强技术

    2024-03-25 11:02:05       39 阅读