8000 add 284 · louisfeng/Leetcode@6bdac69 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6bdac69

Browse files
add 284
1 parent baae0b4 commit 6bdac69

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ Your ideas/fixes/algorithms are more than welcome!
201201
|287|[Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/)|[Solution](../master/src/main/java/com/stevesun/solutions/FindtheDuplicateNumber.java)| O(n)|O(1) | Medium|
202202
|286|[Walls and Gates](https://leetcode.com/problems/walls-and-gates/)|[Solution](../master/src/main/java/com/stevesun/solutions/WallsAndGates.java)| O(m*n)|O(g) | Medium| BFS
203203
|285|[Inorder Successor In BST](https://leetcode.com/problems/inorder-successor-in-bst/)|[Solution](../master/src/main/java/com/stevesun/solutions/InorderSuccessorInBST.java)| O(h)|O(1) | Medium|
204+
|284|[Peeking Iterator](https://leetcode.com/problems/peeking-iterator/)|[Solution](../master/src/main/java/com/stevesun/solutions/_284.java)| O(n)|O(n) | Medium| Design
204205
|283|[Move Zeroes](https://leetcode.com/problems/move-zeroes/)|[Solution](../master/src/main/java/com/stevesun/solutions/MoveZeroes.java)| O(n)|O(1) | Easy|
205206
|282|[Expression Add Operators](https://leetcode.com/problems/expression-add-operators/)|[Solution](../master/src/main/java/com/stevesun/solutions/ExpressionAddOperators.java)| O(?)|O(?) | Hard|
206207
|281|[Zigzag Iterator](https://leetcode.com/problems/zigzag-iterator/)|[Solution](../master/src/main/java/com/stevesun/solutions/_281.java)| O(n)|O(n) | Medium|
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.stevesun.solutions;
2+
3+
import java.util.Iterator;
4+
import java.util.LinkedList;
5+
import java.util.Queue;
6+
7+
/**
8+
* 284. Peeking Iterator
9+
*
10+
* 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().
11+
12+
Here is an example. Assume that the iterator is initialized to the beginning of the queue: [1, 2, 3].
13+
14+
Call next() gets you 1, the first element in the queue.
15+
16+
Now you call peek() and it returns 2, the next element. Calling next() after that still return 2.
17+
18+
You call next() the final time and it returns 3, the last element. Calling hasNext() after that should return false.
19+
20+
Follow up: How would you extend your design to be generic and work with all types, not just integer?
21+
*/
22+
public class _284 {
23+
public static class PeekingIterator implements Iterator<Integer> {
24+
25+
private Queue<Integer> queue;
26+
public PeekingIterator(Iterator<Integer> iterator) {
27+
// initialize any member here.
28+
queue = new LinkedList<>();
29+
while (iterator.hasNext()) {
30+
queue.add(iterator.next());
31+
}
32+
}
33+
34+
// Returns the next element in the iteration without advancing the iterator.
35+
public Integer peek() {
36+
return queue.peek();
37+
}
38+
39+
// hasNext() and next() should behave the same as in the Iterator interface.
40+
// Override them if needed.
41+
@Override
42+
public Integer next() {
43+
return queue.poll();
44+
}
45+
46+
@Override
47+
public boolean hasNext() {
48+
return !queue.isEmpty();
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)
0