Nav: << previous: 283.移动零 | next: 285.二叉搜索树中的中序后继 >>
Description
tab: English
<p>Design an iterator that supports the <code>peek</code> operation on an existing iterator in addition to the <code>hasNext</code> and the <code>next</code> operations.</p>
<p>Implement the <code>PeekingIterator</code> class:</p>
<ul>
<li><code>PeekingIterator(Iterator<int> nums)</code> Initializes the object with the given integer iterator <code>iterator</code>.</li>
<li><code>int next()</code> Returns the next element in the array and moves the pointer to the next element.</li>
<li><code>boolean hasNext()</code> Returns <code>true</code> if there are still elements in the array.</li>
<li><code>int peek()</code> Returns the next element in the array <strong>without</strong> moving the pointer.</li>
</ul>
<p><strong>Note:</strong> Each language may have a different implementation of the constructor and <code>Iterator</code>, but they all support the <code>int next()</code> and <code>boolean hasNext()</code> functions.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input</strong>
["PeekingIterator", "next", "peek", "next", "next", "hasNext"]
[[[1, 2, 3]], [], [], [], [], []]
<strong>Output</strong>
[null, 1, 2, 2, 3, false]
<strong>Explanation</strong>
PeekingIterator peekingIterator = new PeekingIterator([1, 2, 3]); // [<u><strong>1</strong></u>,2,3]
peekingIterator.next(); // return 1, the pointer moves to the next element [1,<u><strong>2</strong></u>,3].
peekingIterator.peek(); // return 2, the pointer does not move [1,<u><strong>2</strong></u>,3].
peekingIterator.next(); // return 2, the pointer moves to the next element [1,2,<u><strong>3</strong></u>]
peekingIterator.next(); // return 3, the pointer moves to the next element [1,2,3]
peekingIterator.hasNext(); // return False
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>1 <= nums[i] <= 1000</code></li>
<li>All the calls to <code>next</code> and <code>peek</code> are valid.</li>
<li>At most <code>1000</code> calls will be made to <code>next</code>, <code>hasNext</code>, and <code>peek</code>.</li>
</ul>
<p> </p>
<strong>Follow up:</strong> How would you extend your design to be generic and work with all types, not just integer?
> [!tip]- Hint 1
>
> Think of "looking ahead". You want to cache the next element.
> [!tip]- Hint 2
>
> Is one variable sufficient? Why or why not?
> [!tip]- Hint 3
>
> Test your design with call order of <code>peek()</code> before <code>next()</code> vs <code>next()</code> before <code>peek()</code>.
> [!tip]- Hint 4
>
> For a clean implementation, check out <a href="https://github.com/google/guava/blob/703ef758b8621cfbab16814f01ddcc5324bdea33/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/Iterators.java#L1125" target="_blank">Google's guava library source code</a>.
---
[submissions](https://leetcode.com/problems/peeking-iterator/submissions/) | [solutions](https://leetcode.com/problems/peeking-iterator/solutions/)
tab: 中文
<p>请你在设计一个迭代器,在集成现有迭代器拥有的 <code>hasNext</code> 和 <code>next</code> 操作的基础上,还额外支持 <code>peek</code> 操作。</p>
<p>实现 <code>PeekingIterator</code> 类:</p>
<ul>
<li><code>PeekingIterator(Iterator<int> nums)</code> 使用指定整数迭代器 <code>nums</code> 初始化迭代器。</li>
<li><code>int next()</code> 返回数组中的下一个元素,并将指针移动到下个元素处。</li>
<li><code>bool hasNext()</code> 如果数组中存在下一个元素,返回 <code>true</code> ;否则,返回 <code>false</code> 。</li>
<li><code>int peek()</code> 返回数组中的下一个元素,但 <strong>不</strong> 移动指针。</li>
</ul>
<p><strong>注意:</strong>每种语言可能有不同的构造函数和迭代器 <code>Iterator</code>,但均支持 <code>int next()</code> 和 <code>boolean hasNext()</code> 函数。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong>
["PeekingIterator", "next", "peek", "next", "next", "hasNext"]
[[[1, 2, 3]], [], [], [], [], []]
<strong>输出:</strong>
[null, 1, 2, 2, 3, false]
<strong>解释:</strong>
PeekingIterator peekingIterator = new PeekingIterator([1, 2, 3]); // [<u><strong>1</strong></u>,2,3]
peekingIterator.next(); // 返回 1 ,指针移动到下一个元素 [1,<u><strong>2</strong></u>,3]
peekingIterator.peek(); // 返回 2 ,指针未发生移动 [1,<u><strong>2</strong></u>,3]
peekingIterator.next(); // 返回 2 ,指针移动到下一个元素 [1,2,<u><strong>3</strong></u>]
peekingIterator.next(); // 返回 3 ,指针移动到下一个元素 [1,2,3]
peekingIterator.hasNext(); // 返回 False
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>1 <= nums[i] <= 1000</code></li>
<li>对 <code>next</code> 和 <code>peek</code> 的调用均有效</li>
<li><code>next</code>、<code>hasNext</code> 和 <code>peek </code>最多调用 <code>1000</code> 次</li>
</ul>
<p> </p>
<p><strong>进阶:</strong>你将如何拓展你的设计?使之变得通用化,从而适应所有的类型,而不只是整数型?</p>
> [!tip]- 提示 1
>
> Think of "looking ahead". You want to cache the next element.
> [!tip]- 提示 2
>
> Is one variable sufficient? Why or why not?
> [!tip]- 提示 3
>
> Test your design with call order of <code>peek()</code> before <code>next()</code> vs <code>next()</code> before <code>peek()</code>.
> [!tip]- 提示 4
>
> For a clean implementation, check out <a href="https://github.com/google/guava/blob/703ef758b8621cfbab16814f01ddcc5324bdea33/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/Iterators.java#L1125" target="_blank">Google's guava library source code</a>.
---
[提交记录](https://leetcode.cn/problems/peeking-iterator/submissions/) | [题解](https://leetcode.cn/problems/peeking-iterator/solution/)
Solutions & Notes
properties:
note.updated:
displayName: Last Updated
note.relative_links:
displayName: Related Links
note.desc:
displayName: Description
note.grade:
displayName: Rating
note.program_language:
displayName: Language
note.time_complexity:
displayName: TC
note.space_complexity:
displayName: SC
views:
- type: table
name: Solutions & Notes
filters:
and:
- file.hasLink(this.file)
- file.tags.containsAny("leetcode/solution", "leetcode/note")
order:
- file.name
- desc
- program_language
- time_complexity
- space_complexity
- grade
- relative_links
- updated
sort:
- property: grade
direction: ASC
- property: time_complexity
direction: ASC
- property: program_language
direction: ASC
columnSize:
file.name: 104
note.space_complexity: 65
note.grade: 126
Similar Problems
properties:
note.lcTopics:
displayName: Topics
note.lcAcRate:
displayName: AC Rate
note.favorites:
displayName: Favorites
note.grade:
displayName: Rating
note.translatedTitle:
displayName: Title (CN)
note.lcDifficulty:
displayName: Difficulty
views:
- type: table
name: Similar Problems
filters:
and:
- file.hasLink(this.file)
- similarQuestions.contains(this.file)
order:
- file.name
- translatedTitle
- lcTopics
- lcDifficulty
- lcAcRate
- grade
- favorites
sort:
- property: file.name
direction: ASC
- property: lcTopics
direction: DESC
columnSize:
note.translatedTitle: 240
note.lcTopics: 347
note.lcAcRate: 75
note.grade: 122