Skip to content

Commit 234fbba

Browse files
author
Botao Xiao
committed
[Function add]: 1. Add leetcode solutions.
1 parent b127012 commit 234fbba

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,10 @@
451451

452452
[283. Move Zeroes](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/283.%20Move%20Zeroes.md)
453453

454+
[284. Peeking Iterator](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/284.%20Peeking%20Iterator.md)
455+
456+
[287. Find the Duplicate Number](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/287.%20Find%20the%20Duplicate%20Number.md)
457+
454458

455459
## Algorithm(4th_Edition)
456460
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.

leetcode/284. Peeking Iterator.md

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ Example:
99
Assume that the iterator is initialized to the beginning of the list: [1,2,3].
1010
1111
Call next() gets you 1, the first element in the list.
12-
Now you call peek() and it returns 2, the next element. Calling next() after that still return 2.
13-
You call next() the final time and it returns 3, the last element.
12+
Now you call peek() and it returns 2, the next element. Calling next() after that still return 2.
13+
You call next() the final time and it returns 3, the last element.
1414
Calling hasNext() after that should return false.
1515
```
1616

@@ -62,4 +62,40 @@ class PeekingIterator implements Iterator<Integer> {
6262
return it.hasNext();
6363
}
6464
}
65-
```
65+
```
66+
67+
### Second time
68+
1. I used a queue to save the values that can be gotten from the iterator.
69+
2. peek means getting the first value in the queue.
70+
3. next means poll the queue.
71+
```Java
72+
// Java Iterator interface reference:
73+
// https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
74+
class PeekingIterator implements Iterator<Integer> {
75+
Iterator<Integer> it = null;
76+
LinkedList<Integer> record = null;
77+
public PeekingIterator(Iterator<Integer> iterator) {
78+
// initialize any member here.
79+
this.it = iterator;
80+
record = new LinkedList<>();
81+
while(it.hasNext()){
82+
int num = it.next();
83+
this.record.add(num);
84+
}
85+
}
86+
// Returns the next element in the iteration without advancing the iterator.
87+
public Integer peek() {
88+
return record.get(0);
89+
}
90+
// hasNext() and next() should behave the same as in the Iterator interface.
91+
// Override them if needed.
92+
@Override
93+
public Integer next() {
94+
return record.poll();
95+
}
96+
@Override
97+
public boolean hasNext() {
98+
return this.record.size() > 0;
99+
}
100+
}
101+
```

leetcode/287. Find the Duplicate Number.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,25 @@ class Solution {
5151
return fast;
5252
}
5353
}
54-
```
54+
```
55+
56+
### Second time
57+
1. This question can be compared with the cyclic list question.
58+
2. The difference between this two question is we use the index as the "pointer" in this question.
59+
```Java
60+
class Solution {
61+
public int findDuplicate(int[] nums) {
62+
int slow = 0, fast = 0;
63+
do{
64+
slow = nums[slow];
65+
fast = nums[nums[fast]];
66+
}while(slow != fast);
67+
slow = 0;
68+
while(slow != fast){
69+
slow = nums[slow];
70+
fast = nums[fast];
71+
}
72+
return slow;
73+
}
74+
}
75+
```

0 commit comments

Comments
 (0)