成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

算法之雙指針?lè)?一)

Near_Li / 940人閱讀

摘要:雙指針?lè)ㄔ诤芏鄨?chǎng)景及算法中都有使用主要應(yīng)用的算法題如下一個(gè)數(shù)組中有奇數(shù)也有偶數(shù),將所有奇數(shù)放到數(shù)組左邊,將所有偶數(shù)放到數(shù)組右邊奇數(shù)偶數(shù)時(shí)間復(fù)雜度一個(gè)數(shù)組中既有奇數(shù)也有偶數(shù),奇數(shù)的下標(biāo)是奇數(shù),偶數(shù)的下標(biāo)是偶數(shù)偶數(shù)奇數(shù)如果數(shù)組最后一個(gè)是

   雙指針?lè)ㄔ诤芏鄨?chǎng)景及算法中都有使用
   主要應(yīng)用的算法題如下:

一個(gè)數(shù)組中有奇數(shù)也有偶數(shù),將所有奇數(shù)放到數(shù)組左邊,將所有偶數(shù)放到數(shù)組右邊

int[] array = {2, 1, 3, 6, 4, 5, 7, 9};
int i = 0;
int j = array.lentgh - 1;
while (i < j){
    while(i

時(shí)間復(fù)雜度O(n)

一個(gè)數(shù)組中既有奇數(shù)也有偶數(shù),奇數(shù)的下標(biāo)是奇數(shù),偶數(shù)的下標(biāo)是偶數(shù)

int[] array = {1, 2, 3, 6, 4, 8, 7, 9};
int even = 0; //偶數(shù)
int odd = 1;  //奇數(shù)
int end = array.length - 1;
while(even <= end && odd <= end){
    if((array[end] & 1) == 0){ //如果數(shù)組最后一個(gè)是偶數(shù)
        swap(array, even, end);
        even+=2;
    }else{
        swap(array, odd, end);
        odd+=2;
    }
}
for(int i =0; i

時(shí)間復(fù)雜度O(n)

求一個(gè)有序數(shù)組中和等于8的下標(biāo)

int[] array = {1, 2, 3, 4, 5, 6 ,7, 8};
int i = 0;
int j = array.length - 1;
while(i < j){
    if(array[i] + array[j] == 8){
        System.out.println("i="+i+" j="+j);
        i++;
        j--;
    }else if(array[i] + array[j] > 8){
        j--;
    }else{
        i++;
    }
}

時(shí)間復(fù)雜度O(n)

兩個(gè)有序數(shù)組合并且去重合成新數(shù)組

int[] a = {4, 8, 10 ,28};
int[] b = {3, 9, 10, 17, 28, 30, 40};
int[] c = new int[a.length+b.length];
int i = 0; int j = 0; int k = 0;
while(i < a.length && j < b.length){
    if(a[i] < b[j]){
        c[k++] = a[i++];
    }else if(a[i] == b[j]){//去重
        c[k++] = a[i];
        i++;
        j++;
    }else{
        c[k++] = b[j++];
    }
}
while(i < a.length){
   c[k++] = a[i++];
}

while(j < b.length){
    c[k++] = b[j++];
}
for(int m = 0; m

時(shí)間復(fù)雜度O(n)

快速排序

int[] array = {3, 4, 1, 7, 6, 2, 0};
sortCore(array, 0, array.length -1);

private static void sortCore(int[] array, int startIndex, int endIndex) {
    if(startIndex > endIndex){
        return;
    }

    int boundray = boundray(array, startIndex, endIndex);

    sortCore(array, startIndex, boundray - 1);
    sortCore(array, boundray + 1, endIndex);
}

private static int boundray(int[] array, int startIndex, int endIndex) {
    int standard = array[startIndex];
    int leftIndex = startIndex;
    int rightIndex = endIndex;

    while(leftIndex < rightIndex){
        while(leftIndex < rightIndex && array[rightIndex] >= standard){
            rightIndex--;
        }
        array[leftIndex] = array[rightIndex];

        while(leftIndex < rightIndex && array[leftIndex] <= standard){
            leftIndex++;
        }
        array[rightIndex] = array[leftIndex];
    }
    array[leftIndex] = standard;
    return leftIndex;
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/71081.html

相關(guān)文章

  • LeetCode 之 JavaScript 解答第344題 —— 反轉(zhuǎn)字符串(Reverse Str

    摘要:小鹿題目反轉(zhuǎn)字符串編寫(xiě)一個(gè)函數(shù),其作用是將輸入的字符串反轉(zhuǎn)過(guò)來(lái)。輸入字符串以字符數(shù)組的形式給出。如果為奇數(shù),當(dāng)兩個(gè)指針相等時(shí),反轉(zhuǎn)完畢。測(cè)試用例空字符串。奇數(shù)個(gè)數(shù)的字符串。長(zhǎng)度為的字符串??疾閮?nèi)容對(duì)字符串的基本操作。 Time:2019/4/18Title: Reverse StringDifficulty: EasyAuthor: 小鹿 題目:Reverse String(反轉(zhuǎn)字...

    bbbbbb 評(píng)論0 收藏0
  • 兩數(shù)之和問(wèn)題各變種多解法小結(jié)

    摘要:兩數(shù)之和問(wèn)題各變種多解法小結(jié)聲明文章均為本人技術(shù)筆記,轉(zhuǎn)載請(qǐng)注明出處兩數(shù)之和等于題目大意給出未排序數(shù)組和指定目標(biāo),返回?cái)?shù)組中兩數(shù)之和的組合元素下標(biāo)要求下標(biāo)從開(kāi)始,而且,保證題目中有且只有個(gè)可行解解法暴力時(shí)間復(fù)雜度求解解題思路暴力二重循環(huán)求解 兩數(shù)之和問(wèn)題各變種多解法小結(jié) 聲明 文章均為本人技術(shù)筆記,轉(zhuǎn)載請(qǐng)注明出處:[1] https://segmentfault.com/u/yzwal...

    lentoo 評(píng)論0 收藏0
  • [Leetcode] 3Sum 4Sum 3Sum Closet 多數(shù)和

    摘要:為了避免得到重復(fù)結(jié)果,我們不僅要跳過(guò)重復(fù)元素,而且要保證找的范圍要是在我們最先選定的那個(gè)數(shù)之后的。而計(jì)算則同樣是先選一個(gè)數(shù),然后再剩下的數(shù)中計(jì)算。 2Sum 在分析多數(shù)和之前,請(qǐng)先看Two Sum的詳解 3Sum 請(qǐng)參閱:https://yanjia.me/zh/2019/01/... 雙指針?lè)?復(fù)雜度 時(shí)間 O(N^2) 空間 O(1) 思路 3Sum其實(shí)可以轉(zhuǎn)化成一個(gè)2Sum的題,...

    trigkit4 評(píng)論0 收藏0
  • [Leetcode] Remove Duplicates from Sorted Array 移除有

    摘要:雙指針?lè)◤?fù)雜度時(shí)間空間思路我們可以將不重復(fù)的序列存到數(shù)列前面,因?yàn)椴恢貜?fù)序列的長(zhǎng)度一定小于等于總序列,所以不用擔(dān)心覆蓋的問(wèn)題。代碼雙指針?lè)◤?fù)雜度時(shí)間空間思路思路和上題一樣,區(qū)別在于記錄前兩個(gè)遍歷到的數(shù)字來(lái)幫助我們判斷是否出現(xiàn)了第三遍。 Remove Duplicates from Sorted Array I Given a sorted array, remove the dupl...

    kel 評(píng)論0 收藏0
  • [LintCode/LeetCode] Trapping Rain Water [棧和雙指針]

    摘要:一種是利用去找同一層的兩個(gè)邊,不斷累加寄存。雙指針?lè)ǖ乃枷胂日业阶笥覂蛇叺牡谝粋€(gè)峰值作為參照位,然后分別向后向前每一步增加該位與參照位在這一位的差值,加入,直到下一個(gè)峰值,再更新為新的參照位。 Problem Given n non-negative integers representing an elevation map where the width of each bar i...

    bluesky 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<