最近看到百度一道面試題:如何從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));
}
写评论
很抱歉,必須登入網站才能發佈留言。