您的位置:首页 > 职场人生

面试题:如何从三亿个整数里面找出不重复的数字的个数

2013-07-07 13:27 323 查看
最近看到百度一道面试题:如何从2.5亿个整数里面找出不重复的数字的个数

写了如下实现,时间略紧,想法略粗,有错误还请各位指出!谢谢。

int[] isRepeat = new int[arr.length];修改为int[] isRepeat = new int[250000000];

需申请jvm 512M的内存

public static int getCount(int[] arr) {
int norepeatCount = 0;//不重复的数字数量
// int repeatCount = 0;//重复出现多次的数量

int[] isRepeat = new int[arr.length];
for (int i : isRepeat) {
isRepeat[i]=0;
}
// 读入一个数,查看相应flag是否为0,如果为0,flag置1,count加1,如果为1,不做处理
for (int i = 0; i < arr.length; i++) {
int count = 1;
if(isRepeat[arr[i]] == 0){
isRepeat[arr[i]] += count;
} else if (isRepeat[arr[i]] > 0) {
isRepeat[arr[i]] += count ;
}
}
for (int i : isRepeat) {
if (i == 1) {
norepeatCount++;//返回的是出现一次的
}
// else if (i > 1) {
// repeatCount++;////返回重复出现多次的数量
// }
}
return norepeatCount;
}

public static void main(String[] args) throws ParseException {
int[] arr = { 1, 2, 2, 3, 4, 5, 6, 7, 7, 8, 8, 8, 8, 9, 10, 10 };
System.out.println(getCount(arr));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐