-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_11.java
More file actions
37 lines (35 loc) · 968 Bytes
/
Copy pathDay_11.java
File metadata and controls
37 lines (35 loc) · 968 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Find All Numbers Disappeared in an Array
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
int n = nums.length;
List<Integer> list = new ArrayList<>();
for(int i=0;i<n;i++){
int idx = Math.abs(nums[i]) - 1;
if(nums[idx] < 0) continue;
else nums[idx] = - nums[idx];
}
for(int i=0;i<n;i++){
if(nums[i] > 0) list.add(i+1);
}
return list;
}
}
// Find the Duplicate Number
class Solution {
public int findDuplicate(int[] nums) {
int n = nums.length;
if(n == 1) return nums[0];
int slow = nums[0], fast = nums[0];
while(true){
slow = nums[slow];
fast = nums[nums[fast]];
if(slow == fast) break;
}
fast = nums[0];
while(slow != fast){
fast = nums[fast];
slow = nums[slow];
}
return fast;
}
}