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

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

找到最長的重復(fù)序列

找到最長的重復(fù)序列

瀟湘沐 2021-09-15 16:05:55
有這樣一個問題。我有一個方法可以將文件中的字節(jié)讀入一個數(shù)組,以及在這個數(shù)組中搜索最長字節(jié)序列的方法。private int element;private int lastElement;private int length;private byte[] readByteFromFile(File name) throws IOException {            return Files.readAllBytes(name.toPath());        }private void searchByte(byte[] byteMass) {    for (int i = 0; i < byteMass.length; i++) {        int count = 0;        for (int j = i + 1; j < byteMass.length; j++) {            if (byteMass[i + count] == byteMass[j]) {                if (count >= length) {                    length = count + 1;                    element = i;                    lastElement = j - count;                }                count++;            } else {                count = 0;            }        }    }}假設(shè)我的文件包含這樣一個數(shù)字序列:444478126354444在處理的情況下,我的方法將推斷出第一次出現(xiàn)在 0,第二次出現(xiàn)在 11 和序列長度 = 4但如果我有這樣一個序列133333444478126354444然后我的方法會推斷出第一次出現(xiàn)在1,第二次出現(xiàn)在2,序列的長度為4如何修復(fù),該方法繼續(xù)正常工作?
查看完整描述

2 回答

?
隔江千里

TA貢獻1906條經(jīng)驗 獲得超10個贊

如果你還沒有準備好,我認為這是非常重要的,以描繪出你的代碼的邏輯!在尋求幫助之前嘗試這樣做非常重要。如果你依靠別人來解決你自己的邏輯,你作為一個程序員不會有太大的進步。


話雖如此,讓我們深入研究并在您的代碼與問題輸入一起運行時進行跟蹤(這不是實際代碼,我們只是在程序運行時查看這些值)


byteMass = 133333444478126354444

(byteMass.length = 21)

length = 0

    0 (i) < 21 (byteMass.length): true

        count = 0

        1 (j) < 21: true

            1 (byteMass[0 (i + count)]) == 3 (byteMass[1 (j)]): false

                count = 0

        2 (j) < 21: true

            1 (byteMass[0 (i + count)]) == 3 (byteMass[2 (j)]): false

                count = 0

        3 (j) < 21: true

            1 == 3: false

它繼續(xù)這樣,但是當 j = 12 時會發(fā)生一些有趣的事情


        12 (j) < 21: true

            1 (byteMass[0 (i + count)]) == 1 (byteMass[12 (j)]): true

                0 (count) >= 0 (length): true

                    length = 1 (count + 1)

                    element = 0 (i)

                    lastElement = 12 (j - count)

                count = 1

至少對我來說,這看起來像是出乎意料的行為!我們想統(tǒng)計重復(fù)的數(shù)字,但是這個1和前面的1相差11位!我們可以通過像這樣編輯內(nèi)部 for 循環(huán)來解決這個問題


for (int j = i + 1; j < byteMass.length && byteMass[i] == byteMass[j]; j++) {

這樣,一旦byteMass[i] == byteMass[j]評估為,內(nèi)部循環(huán)就會中斷false。現(xiàn)在讓我們用新的內(nèi)部 for 循環(huán)重新啟動我們的進程


byteMass = 133333444478126354444

(byteMass.length = 21)

length = 0

    0 (i) < 21 (byteMass.length): true

        count = 0

        1 (j) < 21 && 1 (byteMass[0 (i)]) == 3 (byteMass[1 (j)]): false

    1 (i) < 21: true

        count = 0

        2 (j) < 21 && 3 (byteMass[1 (i)]) == 3 (byteMass[2 (j)]): true

            0 (count) >= 0 (length): true

                length = 1 (0 (count) + 1)

                element = 1 (i)

                lastElement = 2 (2 (j) - 0 (count))

            count = 1 (0 (count) + 1)

        3 (j) < 21 && 3 (byteMass[2 (1 (i) + 1 (count))]) == 3 (byteMass[3 (j)]): true

            1 (count) >= 1 (length): true

                length = 2 (1 (count) + 1)

                element = 1 (i)

                lastElement = 2 (3 (j) - 1 (count))

這對我來說似乎是意外的行為,但我不會修復(fù)它,因為我不知道如何:我不知道 element 和 lastElement 代表什么。代碼繼續(xù)這樣直到 j = 6:


        6 (j) < 21 && 3 (byteMass[5 (1 (i) + 4 (count))]) == 4 (bteMass[3 (j)]): false

    2 (i) < 21: true

        count = 0

        3 (j) < 21: true

            3 (byteMass[2 (2 (i) + 0 (count))]) == 3 (byteMass[3 (j)]): true

                length = 1 (0 (count) + 1)

                element = 2 (i)

                lastElement = 3 (3 (j) - 1 (count))

            count = 1 (0 (count) + 1)

這再次以相同的方式繼續(xù),直到 j = 6。此時希望您能明白為什么您的程序沒有按預(yù)期工作。但是我仍然沒有回答如何修復(fù)它的問題。我不太明白你如何解決這個問題的思考過程,但我會和你分享我自己的


首先,我們需要將問題分解成更小的塊!


你可以用任何你想要的方式做到這一點,但這是我的方式:我們的目標是找到最長的重復(fù)模式。那么為了做到這一點,我們需要弄清楚


一個數(shù)字何時重復(fù)以及它重復(fù)了多少次

如果該特定數(shù)字在序列中的其他任何地方重復(fù)特定次數(shù)。如果是這樣,我們需要節(jié)省它重復(fù)的次數(shù)

然后我們將重復(fù)該過程,但僅當重復(fù)次數(shù)大于保存的數(shù)據(jù)時才保存數(shù)據(jù)

這實際上是一種復(fù)雜的問題,老實說,使用輔助函數(shù)可能更容易解決。我希望這有幫助!


查看完整回答
反對 回復(fù) 2021-09-15
?
胡子哥哥

TA貢獻1825條經(jīng)驗 獲得超6個贊

它沒有經(jīng)過測試。在我面前沒有IDE。原始代碼的變化是。第二個循環(huán)少迭代一個元素。如果下一個元素不等于前一個元素,則循環(huán)退出。


private void searchByte(byte[] byteMass) {

        int maxLength = 0

        int element;

        for (int i = 0; i < byteMass.length; i++) {

            int count = 0;

            for (int j = i + 1; j < byteMass.length-1; j++) {

                if (byteMass[i] == byteMass[j]) {

                    if (count > length) {

                        maxLength = count;

                        element = i;

                    }

                    count++;

                } else {


                    break;

                 }

            }

        }


查看完整回答
反對 回復(fù) 2021-09-15
  • 2 回答
  • 0 關(guān)注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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