Skip to content

Commit c1b6ae8

Browse files
committed
feat: add solutions to lc problem: No.0284.Peeking Iterator
1 parent 4d59c31 commit c1b6ae8

File tree

7 files changed

+319
-9
lines changed

7 files changed

+319
-9
lines changed

solution/0200-0299/0284.Peeking Iterator/README.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
<!-- 这里可写通用的实现逻辑 -->
2525

26+
定义一个变量 peekElement 专门用来保存下一个值,布尔变量 hasPeeked 标记是否保存了下一个元素。
27+
2628
<!-- tabs:start -->
2729

2830
### **Python3**
@@ -143,6 +145,113 @@ class PeekingIterator implements Iterator<Integer> {
143145
}
144146
```
145147

148+
### **C++**
149+
150+
```cpp
151+
/*
152+
* Below is the interface for Iterator, which is already defined for you.
153+
* **DO NOT** modify the interface for Iterator.
154+
*
155+
* class Iterator {
156+
* struct Data;
157+
* Data* data;
158+
* public:
159+
* Iterator(const vector<int>& nums);
160+
* Iterator(const Iterator& iter);
161+
*
162+
* // Returns the next element in the iteration.
163+
* int next();
164+
*
165+
* // Returns true if the iteration has more elements.
166+
* bool hasNext() const;
167+
* };
168+
*/
169+
170+
class PeekingIterator : public Iterator {
171+
public:
172+
PeekingIterator(const vector<int>& nums) : Iterator(nums) {
173+
// Initialize any member here.
174+
// **DO NOT** save a copy of nums and manipulate it directly.
175+
// You should only use the Iterator interface methods.
176+
hasPeeked = false;
177+
}
178+
179+
// Returns the next element in the iteration without advancing the iterator.
180+
int peek() {
181+
if (!hasPeeked)
182+
{
183+
peekedElement = Iterator::next();
184+
hasPeeked = true;
185+
}
186+
return peekedElement;
187+
}
188+
189+
// hasNext() and next() should behave the same as in the Iterator interface.
190+
// Override them if needed.
191+
int next() {
192+
if (!hasPeeked) return Iterator::next();
193+
hasPeeked = false;
194+
return peekedElement;
195+
}
196+
197+
bool hasNext() const {
198+
return hasPeeked || Iterator::hasNext();
199+
}
200+
private:
201+
bool hasPeeked;
202+
int peekedElement;
203+
};
204+
```
205+
206+
### **Go**
207+
208+
```go
209+
/* Below is the interface for Iterator, which is already defined for you.
210+
*
211+
* type Iterator struct {
212+
*
213+
* }
214+
*
215+
* func (this *Iterator) hasNext() bool {
216+
* // Returns true if the iteration has more elements.
217+
* }
218+
*
219+
* func (this *Iterator) next() int {
220+
* // Returns the next element in the iteration.
221+
* }
222+
*/
223+
224+
type PeekingIterator struct {
225+
iter *Iterator
226+
hasPeeked bool
227+
peekedElement int
228+
}
229+
230+
func Constructor(iter *Iterator) *PeekingIterator {
231+
return &PeekingIterator{iter, iter.hasNext(), iter.next()}
232+
}
233+
234+
func (this *PeekingIterator) hasNext() bool {
235+
return this.hasPeeked || this.iter.hasNext()
236+
}
237+
238+
func (this *PeekingIterator) next() int {
239+
if !this.hasPeeked {
240+
return this.iter.next()
241+
}
242+
this.hasPeeked = false
243+
return this.peekedElement
244+
}
245+
246+
func (this *PeekingIterator) peek() int {
247+
if !this.hasPeeked {
248+
this.peekedElement = this.iter.next()
249+
this.hasPeeked = true
250+
}
251+
return this.peekedElement
252+
}
253+
```
254+
146255
### **...**
147256

148257
```

solution/0200-0299/0284.Peeking Iterator/README_EN.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,113 @@ class PeekingIterator implements Iterator<Integer> {
165165
}
166166
```
167167

168+
### **C++**
169+
170+
```cpp
171+
/*
172+
* Below is the interface for Iterator, which is already defined for you.
173+
* **DO NOT** modify the interface for Iterator.
174+
*
175+
* class Iterator {
176+
* struct Data;
177+
* Data* data;
178+
* public:
179+
* Iterator(const vector<int>& nums);
180+
* Iterator(const Iterator& iter);
181+
*
182+
* // Returns the next element in the iteration.
183+
* int next();
184+
*
185+
* // Returns true if the iteration has more elements.
186+
* bool hasNext() const;
187+
* };
188+
*/
189+
190+
class PeekingIterator : public Iterator {
191+
public:
192+
PeekingIterator(const vector<int>& nums) : Iterator(nums) {
193+
// Initialize any member here.
194+
// **DO NOT** save a copy of nums and manipulate it directly.
195+
// You should only use the Iterator interface methods.
196+
hasPeeked = false;
197+
}
198+
199+
// Returns the next element in the iteration without advancing the iterator.
200+
int peek() {
201+
if (!hasPeeked)
202+
{
203+
peekedElement = Iterator::next();
204+
hasPeeked = true;
205+
}
206+
return peekedElement;
207+
}
208+
209+
// hasNext() and next() should behave the same as in the Iterator interface.
210+
// Override them if needed.
211+
int next() {
212+
if (!hasPeeked) return Iterator::next();
213+
hasPeeked = false;
214+
return peekedElement;
215+
}
216+
217+
bool hasNext() const {
218+
return hasPeeked || Iterator::hasNext();
219+
}
220+
private:
221+
bool hasPeeked;
222+
int peekedElement;
223+
};
224+
```
225+
226+
### **Go**
227+
228+
```go
229+
/* Below is the interface for Iterator, which is already defined for you.
230+
*
231+
* type Iterator struct {
232+
*
233+
* }
234+
*
235+
* func (this *Iterator) hasNext() bool {
236+
* // Returns true if the iteration has more elements.
237+
* }
238+
*
239+
* func (this *Iterator) next() int {
240+
* // Returns the next element in the iteration.
241+
* }
242+
*/
243+
244+
type PeekingIterator struct {
245+
iter *Iterator
246+
hasPeeked bool
247+
peekedElement int
248+
}
249+
250+
func Constructor(iter *Iterator) *PeekingIterator {
251+
return &PeekingIterator{iter, iter.hasNext(), iter.next()}
252+
}
253+
254+
func (this *PeekingIterator) hasNext() bool {
255+
return this.hasPeeked || this.iter.hasNext()
256+
}
257+
258+
func (this *PeekingIterator) next() int {
259+
if !this.hasPeeked {
260+
return this.iter.next()
261+
}
262+
this.hasPeeked = false
263+
return this.peekedElement
264+
}
265+
266+
func (this *PeekingIterator) peek() int {
267+
if !this.hasPeeked {
268+
this.peekedElement = this.iter.next()
269+
this.hasPeeked = true
270+
}
271+
return this.peekedElement
272+
}
273+
```
274+
168275
### **...**
169276

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

solution/1600-1699/1626.Best Team With No Conflicts/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,8 @@ class Solution:
6666
def bestTeamScore(self, scores: List[int], ages: List[int]) -> int:
6767
nums = list(zip(scores, ages))
6868
nums.sort(key=lambda x: (x[1], x[0]))
69-
n = len(ages)
70-
dp = [nums[i][0] for i in range(n)]
71-
res = 0
69+
dp = [num[0] for num in nums]
70+
res, n = 0, len(ages)
7271
for i in range(n):
7372
for j in range(i):
7473
if nums[j][0] <= nums[i][0]:

solution/1600-1699/1626.Best Team With No Conflicts/README_EN.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,8 @@ class Solution:
5858
def bestTeamScore(self, scores: List[int], ages: List[int]) -> int:
5959
nums = list(zip(scores, ages))
6060
nums.sort(key=lambda x: (x[1], x[0]))
61-
n = len(ages)
62-
dp = [nums[i][0] for i in range(n)]
63-
res = 0
61+
dp = [num[0] for num in nums]
62+
res, n = 0, len(ages)
6463
for i in range(n):
6564
for j in range(i):
6665
if nums[j][0] <= nums[i][0]:

solution/1600-1699/1626.Best Team With No Conflicts/Solution.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ class Solution:
22
def bestTeamScore(self, scores: List[int], ages: List[int]) -> int:
33
nums = list(zip(scores, ages))
44
nums.sort(key=lambda x: (x[1], x[0]))
5-
n = len(ages)
6-
dp = [nums[i][0] for i in range(n)]
7-
res = 0
5+
dp = [num[0] for num in nums]
6+
res, n = 0, len(ages)
87
for i in range(n):
98
for j in range(i):
109
if nums[j][0] <= nums[i][0]:

0 commit comments

Comments
 (0)