Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b93a4bf

Browse files
committed
"Peeking Iterator"
1 parent 6b23ee0 commit b93a4bf

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ The `☢` means that you need to have a LeetCode Premium Subscription.
3333
| 287 | [Find the Duplicate Number] | |
3434
| 286 | [Walls and Gates]| |
3535
| 285 | [Inorder Successor in BST]| |
36-
| 284 | [Peeking Iterator] | |
36+
| 284 | [Peeking Iterator] | [C++](src/284.cpp) |
3737
| 283 | [Move Zeroes] | [C](src/283.c) |
3838
| 282 | [Expression Add Operators] | |
3939
| 281 | [Zigzag Iterator]| |

‎src/284.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Below is the interface for Iterator, which is already defined for you.
2+
// **DO NOT** modify the interface for Iterator.
3+
class Iterator {
4+
struct Data;
5+
Data* data;
6+
public:
7+
Iterator(const vector<int>& nums);
8+
Iterator(const Iterator& iter);
9+
virtual ~Iterator();
10+
// Returns the next element in the iteration.
11+
int next();
12+
// Returns true if the iteration has more elements.
13+
bool hasNext() const;
14+
};
15+
16+
17+
class PeekingIterator : public Iterator {
18+
bool hasPeeked;
19+
int peekElement;
20+
public:
21+
PeekingIterator(const vector<int>& nums) : Iterator(nums) {
22+
// Initialize any member here.
23+
// **DO NOT** save a copy of nums and manipulate it directly.
24+
// You should only use the Iterator interface methods.
25+
peekElement = 0;
26+
hasPeeked = false;
27+
}
28+
29+
// Returns the next element in the iteration without advancing the iterator.
30+
int peek() {
31+
if (!hasPeeked) {
32+
peekElement = Iterator::next();
33+
hasPeeked = true;
34+
}
35+
return peekElement;
36+
}
37+
38+
// hasNext() and next() should behave the same as in the Iterator interface.
39+
// Override them if needed.
40+
int next() {
41+
if (hasPeeked) {
42+
hasPeeked = false;
43+
return peekElement;
44+
}
45+
return Iterator::next();
46+
}
47+
48+
bool hasNext() const {
49+
if (hasPeeked) return true;
50+
return Iterator::hasNext();
51+
}
52+
};
53+
54+
/*
55+
class PeekingIterator : public Iterator {
56+
public:
57+
PeekingIterator(const vector<int>& nums) : Iterator(nums) {
58+
// Initialize any member here.
59+
// **DO NOT** save a copy of nums and manipulate it directly.
60+
// You should only use the Iterator interface methods.
61+
}
62+
63+
// Returns the next element in the iteration without advancing the iterator.
64+
int peek() {
65+
Iterator(*this).next();
66+
}
67+
68+
// hasNext() and next() should behave the same as in the Iterator interface.
69+
// Override them if needed.
70+
int next() {
71+
return Iterator::next();
72+
}
73+
74+
bool hasNext() const {
75+
return Iterator::hasNext();
76+
}
77+
};
78+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /