-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path287-findDupliNum.java
More file actions
40 lines (39 loc) · 1.08 KB
/
287-findDupliNum.java
File metadata and controls
40 lines (39 loc) · 1.08 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
class Solution_287 {
// 二分法,比
public int findDuplicate(int[] nums) {
int len = nums.length;
if(len == 0) return 0;
int left = 1, right=nums.length-1;
while(left < right) {
int mid = (left+right)/2;
int count = 0;
for(int num:nums ) {
if(num <= mid) {
count++;
}
}
if(count > mid) right = mid;
else left=mid+1;
}
return left;
}
// 快慢指针做法,想像成带环的链表,数组的下标就是指针,找的是入口值,类似于带环的链表的问题
public int findDuplicate_2(int[] nums) {
int len = nums.length;
if(len == 0) return 0;
int fast=0, slow=0;
while(true) {
fast = nums[nums[fast]];
slow = nums[slow];
if(slow == fast) {
break;
}
}
fast = 0;
while(fast!=slow) {
slow = nums[slow];
fast = nums[fast];
}
return slow;
}
}