Skip to content

Commit 31b2529

Browse files
committed
solve problem Peeking Iterator
1 parent 356c7b7 commit 31b2529

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ All solutions will be accepted!
238238
|654|[Maximum Binary Tree](https://leetcode-cn.com/problems/maximum-binary-tree/description/)|[java/py/js](./algorithms/MaximumBinaryTree)|Medium|
239239
|208|[Implement Trie Prefix Tree](https://leetcode-cn.com/problems/implement-trie-prefix-tree/description/)|[java/py/js](./algorithms/ImplementTriePrefixTree)|Medium|
240240
|114|[Flatten Binary Tree To Linked List](https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/description/)|[java/py/js](./algorithms/FlattenBinaryTreeToLinkedList)|Medium|
241+
|284|[Peeking Iterator](https://leetcode-cn.com/problems/peeking-iterator/description/)|[java/py](./algorithms/PeekingIterator)|Medium|
241242

242243
# Database
243244
|#|Title|Solution|Difficulty|

algorithms/PeekingIterator/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Peeking Iterator
2+
This problem is easy to solve
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Java Iterator interface reference:
2+
// https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
3+
class PeekingIterator implements Iterator<Integer> {
4+
private Iterator<Integer> iterator;
5+
private Integer top;
6+
public PeekingIterator(Iterator<Integer> iterator) {
7+
// initialize any member here.
8+
this.iterator = iterator;
9+
top = iterator.hasNext() ? iterator.next() : null;
10+
}
11+
12+
// Returns the next element in the iteration without advancing the iterator.
13+
public Integer peek() {
14+
return top;
15+
}
16+
17+
// hasNext() and next() should behave the same as in the Iterator interface.
18+
// Override them if needed.
19+
@Override
20+
public Integer next() {
21+
Integer temp = top;
22+
top = iterator.hasNext() ? iterator.next() : null;
23+
return temp;
24+
}
25+
26+
@Override
27+
public boolean hasNext() {
28+
return top != null;
29+
}
30+
}
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Below is the interface for Iterator, which is already defined for you.
2+
#
3+
# class Iterator(object):
4+
# def __init__(self, nums):
5+
# """
6+
# Initializes an iterator object to the beginning of a list.
7+
# :type nums: List[int]
8+
# """
9+
#
10+
# def hasNext(self):
11+
# """
12+
# Returns true if the iteration has more elements.
13+
# :rtype: bool
14+
# """
15+
#
16+
# def next(self):
17+
# """
18+
# Returns the next element in the iteration.
19+
# :rtype: int
20+
# """
21+
22+
class PeekingIterator(object):
23+
def __init__(self, iterator):
24+
"""
25+
Initialize your data structure here.
26+
:type iterator: Iterator
27+
"""
28+
self.iterator = iterator
29+
self.top = iterator.next() if iterator.hasNext() else None
30+
31+
def peek(self):
32+
"""
33+
Returns the next element in the iteration without advancing the iterator.
34+
:rtype: int
35+
"""
36+
return self.top
37+
38+
def next(self):
39+
"""
40+
:rtype: int
41+
"""
42+
top = self.top
43+
self.top = self.iterator.next() if self.iterator.hasNext() else None
44+
return top
45+
46+
def hasNext(self):
47+
"""
48+
:rtype: bool
49+
"""
50+
return self.top != None
51+
52+
# Your PeekingIterator object will be instantiated and called as such:
53+
# iter = PeekingIterator(Iterator(nums))
54+
# while iter.hasNext():
55+
# val = iter.peek() # Get the next element but not advance the iterator.
56+
# iter.next() # Should return the same value as [val].

0 commit comments

Comments
 (0)