-
Notifications
You must be signed in to change notification settings - Fork 0
/
287.寻找重复数.java
44 lines (38 loc) · 944 Bytes
/
287.寻找重复数.java
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
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/*
* @lc app=leetcode.cn id=287 lang=java
*
* [287] 寻找重复数
*/
// @lc code=start
class Solution {
//[3,1,3,4,2]
public int findDuplicate(int[] nums) {
int len = nums.length; // 5
int right = len-1;// 4
int left = 0;
int mid = 0;
int count = 0;
while(left < right){// 0 < 4 | 3 < 4
mid = (right - left) / 2 + left; // 2 | 3
for(int i = 0; i < len; i++){
if(nums[i] <= mid){
count++;
}
}
if(count <= mid){// 2 <= 2
left = mid + 1; // 3
}else{ // 4 > 3
right = mid; // 3
}
count = 0;
}
return left;
}
}
// @lc code=end