-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_3FindRepeatNumber.java
More file actions
61 lines (56 loc) · 1.61 KB
/
_3FindRepeatNumber.java
File metadata and controls
61 lines (56 loc) · 1.61 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
61
package offer;
import java.util.HashSet;
import java.util.Set;
/**
* @author : CodeWater
* @create :2022-03-09-15:51
* @Function Description :剑指offer 03
* 数组中重复的数字
*/
public class _3FindRepeatNumber {
//方法一:开数组存
public int findRepeatNumber(int[] nums) {
int[] count = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
count[nums[i]]++;
}
for (int i = 0; i < nums.length; i++) {
if (count[i] >= 2) {
return i;
}
}
//没有重复
return 0;
}
/*用set集合的不可重复*/
public int findRepeatNumber2(int[] nums) {
Set<Integer> set = new HashSet<Integer>();
int repeat = -1;
for (int num : nums) {
if (!set.add(num)) {
repeat = num;
break;
}
}
return repeat;
}
/*方法三: 比较交换的。。。==================================================*/
// 把下标和数组存储的元素一一对应
public int findRepeatNumber3(int[] nums) {
int i = 0 ;
while( i < nums.length ) {
// 下标和当前元素正好相等,下一个
if( nums[i] == i ){
i++;
continue;
}
// 两个不同下标的数组元素相同
if( nums[nums[i]] == nums[i] ) return nums[i];
// 不同就交换!!!
int temp = nums[i];
nums[i] = nums[temp];
nums[temp] = temp;
}
return -1;
}
}