-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path287.java
More file actions
45 lines (38 loc) · 1.15 KB
/
287.java
File metadata and controls
45 lines (38 loc) · 1.15 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
__________________________________________________________________________________________________
sample 0 ms submission
class Solution {
public int findDuplicate(int[] nums) {
if(nums.length < 2) return -1;
int slow = nums[0];
int fast = nums[nums[0]];
while(slow != fast){
slow = nums[slow];
fast = nums[nums[fast]];
}
fast = 0;
while(slow != fast){
fast = nums[fast];
slow = nums[slow];
}
return slow;
}
}
__________________________________________________________________________________________________
sample 34208 kb submission
class Solution {
public int findDuplicate(int[] nums) {
int lo=0; int hi=nums.length-1;
while(lo<hi){
int c=0;
int mid=(lo+hi)/2;
for(int n:nums){
if (n<=mid)
c+=1;
}
if(c<=mid) lo=mid+1;
else hi=mid;
}
return lo;
}
}
__________________________________________________________________________________________________