第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java數(shù)組,查找重復(fù)項

Java數(shù)組,查找重復(fù)項

回首憶惘然 2019-07-05 14:28:43
Java數(shù)組,查找重復(fù)項我有一個數(shù)組,正在尋找副本。duplicates = false;for(j = 0; j < zipcodeList.length; j++){     for(k = 0; k < zipcodeList.length; k++){         if (zipcodeList[k] == zipcodeList[j]){             duplicates = true;         }     }}但是,當(dāng)沒有副本時,此代碼不能工作。為什么這么說?
查看完整描述

3 回答

?
蕭十郎

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個贊

讓我們看看您的算法是如何工作的:


an array of unique values:


[1, 2, 3]


check 1 == 1. yes, there is duplicate, assigning duplicate to true.

check 1 == 2. no, doing nothing.

check 1 == 3. no, doing nothing.

check 2 == 1. no, doing nothing.

check 2 == 2. yes, there is duplicate, assigning duplicate to true.

check 2 == 3. no, doing nothing.

check 3 == 1. no, doing nothing.

check 3 == 2. no, doing nothing.

check 3 == 3. yes, there is duplicate, assigning duplicate to true.

更好的算法:


for (j=0;j<zipcodeList.length;j++) {

    for (k=j+1;k<zipcodeList.length;k++) {

        if (zipcodeList[k]==zipcodeList[j]){ // or use .equals()

            return true;

        }

    }

}

return false;


查看完整回答
反對 回復(fù) 2019-07-05
?
素胚勾勒不出你

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超9個贊

您可以使用位圖來提高大數(shù)組的性能。

    java.util.Arrays.fill(bitmap, false);

    for (int item : zipcodeList)
        if (!bitmap[item]) bitmap[item] = true;
        else break;

更新:這是我在過去的一個非常疏忽的回答,把它保存在這里僅供參考。你應(yīng)該參考Andersoj的優(yōu)秀作品回答.


查看完整回答
反對 回復(fù) 2019-07-05
  • 3 回答
  • 0 關(guān)注
  • 760 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號