剑指offer--数组中重复的数字
2017-08-16 15:36
417 查看
题目描述
在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。
public class 数组中重复的数字开始 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// int[] numbers = {2,3,1,0,2,5,3};
int[] numbers = {};
int length =numbers.length;
int[] duplication = new int[1];
System.out.println(duplicate(numbers, length, duplication)+":"+duplication[0]);
}
public static boolean duplicate(int numbers[],int length,int [] duplication) {
if (length!=numbers.length||numbers.length<2||numbers==null) {
duplication[0] = -1;
return false;
}
int[] copynum = new int[length];
for (int i = 0; i < copynum.length; i++) {
copynum[i] = -1;
if (numbers[i]>length-1) {
return false;
}
}
for (int i = 0; i < numbers.length; i++) {
if (copynum[numbers[i]]==-1) {
copynum[numbers[i]]=1;
}else {
copynum[numbers[i]]++;
}
}
for (int i = 0; i < copynum.length; i++) {
if (copynum[i]>1) {
duplication[0]=i;
return true;
}
}
return false;
}
}
别人的做法:
public boolean duplicate(int numbers[], int length, int[] duplication) {
boolean[] k = new boolean[length];
for (int i = 0; i < k.length; i++) {
if (k[numbers[i]] == true) {
duplication[0] = numbers[i];
return true;
}
k[numbers[i]] = true;
}
return false;
}
在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。
public class 数组中重复的数字开始 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// int[] numbers = {2,3,1,0,2,5,3};
int[] numbers = {};
int length =numbers.length;
int[] duplication = new int[1];
System.out.println(duplicate(numbers, length, duplication)+":"+duplication[0]);
}
public static boolean duplicate(int numbers[],int length,int [] duplication) {
if (length!=numbers.length||numbers.length<2||numbers==null) {
duplication[0] = -1;
return false;
}
int[] copynum = new int[length];
for (int i = 0; i < copynum.length; i++) {
copynum[i] = -1;
if (numbers[i]>length-1) {
return false;
}
}
for (int i = 0; i < numbers.length; i++) {
if (copynum[numbers[i]]==-1) {
copynum[numbers[i]]=1;
}else {
copynum[numbers[i]]++;
}
}
for (int i = 0; i < copynum.length; i++) {
if (copynum[i]>1) {
duplication[0]=i;
return true;
}
}
return false;
}
}
别人的做法:
public boolean duplicate(int numbers[], int length, int[] duplication) {
boolean[] k = new boolean[length];
for (int i = 0; i < k.length; i++) {
if (k[numbers[i]] == true) {
duplication[0] = numbers[i];
return true;
}
k[numbers[i]] = true;
}
return false;
}
相关文章推荐
- 剑指offer—数组中重复的数字
- 【剑指Offer-】数组中重复的数字
- 剑指Offer面试题51:数组中的重复数字
- 剑指Offer--051-数组中重复的数字
- 【剑指offer】数组中重复的数字
- 牛客网_剑指offer_数组中重复的数字
- 剑指Offer 51数组中的重复数字 Java版
- 数组中的重复(缺失)数字 剑指offer3 及扩展
- <剑指offer 面试题3> 数组中重复的数字(Java实现)
- 剑指Offer_50_数组中重复的数字
- 剑指offer-数组中重复的数字
- 剑指offer-数组中重复的数字
- 剑指offer——面试题3:找出数组中重复的数字
- 【剑指offer】数组中重复的数字
- (C++)剑指offer-50:数组中重复的数字(数组)
- 剑指offer:数组中重复的数字
- 剑指offer 面试题3:数组中重复的数字
- 剑指Offer面试题3:数组中重复的数字
- 剑指offer--数组中重复的数字
- 剑指offer--(14)数组中重复的数字--Java描述