-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFindtheDuplicateNumber.java
More file actions
60 lines (57 loc) · 1.7 KB
/
FindtheDuplicateNumber.java
File metadata and controls
60 lines (57 loc) · 1.7 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
public class Solution {
public int findDuplicate(int[] nums) {
return linearSearch(nums);
}
public int linearSearch(int[] nums) {
if (nums.length == 0) {
return 0;
}
// Imagine this nums array is a linked list
int slow = 0, fast = 0;
while (true) {
slow = nums[slow];
fast = nums[nums[fast]];
if (slow == fast) {
break;
}
}
fast = 0;
while (true) {
slow = nums[slow];
fast = nums[fast];
if (slow == fast) {
break;
}
}
return slow;
}
public int binarySearch(int[] nums) {
int left = 0, right = nums.length - 1;
while (left < right) {
// find mid point
int mid = left + (right - left) / 2;
// calculate whats below mid
// the general idea is
// if left is too crowded, switch to left
// else switch to right
int lowerCount = 0;
for (int i = 0; i < nums.length; ++i) {
// check if left is too crowded
// remember mid is always the lower bound of the middle number
// i.e. 1, 2, 2, 3
// 1: left = 0, right = 3, mid = 1
// 2: left = 0, right = 1, mid = 0
// 3: left = 1, right = 1, mid = 1
if (nums[i] <= mid) {
++lowerCount;
}
}
if (lowerCount <= mid) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
}