forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT50.java
More file actions
32 lines (30 loc) · 1 KB
/
T50.java
File metadata and controls
32 lines (30 loc) · 1 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
package web; /**
* @program LeetNiu
* @description: 数组中重复的数字
* @author: mf
* @create: 2020/01/16 13:58
*/
import java.util.Arrays;
/**
* 在一个长度为n的数组里的所有数字都在0到n-1的范围内。
* 数组中某些数字是重复的,但不知道有几个数字是重复的。
* 也不知道每个数字重复几次。请找出数组中任意一个重复的数字。
* 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。
*/
public class T50 {
public boolean duplicate(int numbers[],int length,int [] duplication) {
if (numbers == null || numbers.length == 0) {
return false;
}
Arrays.sort(numbers);
int flag = 0;
for (int i = 0; i < length - 1; i++) {
if (numbers[i] == numbers[i + 1]) {
duplication[0] = numbers[i];
flag = 1;
break;
}
}
return flag == 1 ? true : false;
}
}