-
-
Notifications
You must be signed in to change notification settings - Fork 524
/
Copy pathSolution.java
55 lines (52 loc) · 1.28 KB
/
Solution.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
44
45
46
47
48
49
50
51
52
53
54
55
/**
* @author bingo
* @since 2018/12/13
*/
class Solution {
/**
* 不修改数组查找重复的元素,没有则返回0
*
* @param nums 数组
* @return 重复的元素
*/
public int duplicateInArray(int[] nums) {
if (nums == null || nums.length < 2) {
return 0;
}
int start = 1, end = nums.length - 1;
while (start <= end) {
int mid = start + ((end - start) >> 1);
int cnt = getCountRange(nums, start, mid);
if (start == end) {
if (cnt > 1) {
// 找到重复的数字
return start;
}
break;
}
if (cnt > mid - start + 1) {
end = mid;
} else {
start = mid + 1;
}
}
return 0;
}
/**
* 计算整个数组中有多少个数的取值在[from, to] 之间
*
* @param nums 数组
* @param from 左边界
* @param to 右边界
* @return 数量
*/
private int getCountRange(int[] nums, int from, int to) {
int cnt = 0;
for (int e : nums) {
if (e >= from && e <= to) {
++cnt;
}
}
return cnt;
}
}