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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在 C# 中的數(shù)組中查找子數(shù)組

在 C# 中的數(shù)組中查找子數(shù)組

C#
躍然一笑 2022-10-23 16:31:36
我試圖在數(shù)組中找到子數(shù)組。它僅適用于一個(gè)子數(shù)組,但我希望如果有多個(gè)子數(shù)組,它會(huì)返回最后一個(gè)的索引。例如,對(duì)于 [3,4,1,2,0,1,2,5,6] 和 [1,2] 應(yīng)該返回 5。public int FindArray(int[] array, int[] subArray)    {        //throw new NotImplementedException();    int y=0;    int index=0;    bool find= false;    for(int x=0;x< array.Length && y< subArray.Length;)    {        if(array[x]!= subArray[y])        {            if(find==true)            {                               y=0;                index=x;            }            else            {                x++;                                y=0;                index=x;                }        }        else        {            find=true;            x++;                    y++;        }    }    if(y==subArray.Length)            return index;    else            return -1;    }}
查看完整描述

1 回答

?
一只甜甜圈

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊

public int FindLast(int[] haystack, int[] needle)

{

    // iterate backwards, stop if the rest of the array is shorter than needle (i >= needle.Length)

    for (var i = haystack.Length - 1; i >= needle.Length - 1; i--)

    {

        var found = true;

        // also iterate backwards through needle, stop if elements do not match (!found)

        for (var j = needle.Length - 1; j >= 0 && found; j--)

        {

            // compare needle's element with corresponding element of haystack

            found = haystack[i - (needle.Length - 1 - j)] == needle[j];

        }

        if (found)

            // result was found, i is now the index of the last found element, so subtract needle's length - 1

            return i - (needle.Length - 1);

    }

    // not found, return -1

    return -1;

}

作為一個(gè)可運(yùn)行的小提琴:https ://dotnetfiddle.net/TfjPuY


查看完整回答
反對(duì) 回復(fù) 2022-10-23
  • 1 回答
  • 0 關(guān)注
  • 567 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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