算法之雙指針法(一)

雙指針法在不少場景及算法中都有使用
   主要應用的算法題以下:
  1. 一個數組中有奇數也有偶數,將全部奇數放到數組左邊,將全部偶數放到數組右邊算法

    int[] array = {2, 1, 3, 6, 4, 5, 7, 9};
    int i = 0;
    int j = array.lentgh - 1;
    while (i < j){
        while(i<j && array[i] & 0x1 == 1){//奇數
            i++;
        }
        
        while(i<j && array[j] & 0x1 == 0){//偶數
            j--;
        }
        
        if(i < j){
            int temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }
    }

    時間複雜度O(n)數組

  2. 一個數組中既有奇數也有偶數,奇數的下標是奇數,偶數的下標是偶數指針

    int[] array = {1, 2, 3, 6, 4, 8, 7, 9};
    int even = 0; //偶數
    int odd = 1;  //奇數
    int end = array.length - 1;
    while(even <= end && odd <= end){
        if((array[end] & 1) == 0){ //若是數組最後一個是偶數
            swap(array, even, end);
            even+=2;
        }else{
            swap(array, odd, end);
            odd+=2;
        }
    }
    for(int i =0; i<array.length; i++){
        System.out.println(array[i]);
    }
    
    private static void swap(int[] array, int a, int b){
        int temp = array[a];
        array[a] = array[b];
        array[b] = temp;
    }

    時間複雜度O(n)code

  3. 求一個有序數組中和等於8的下標排序

    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++;
        }
    }

    時間複雜度O(n)sort

  4. 兩個有序數組合而且去重合成新數組static

    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<c[m]; m++){
        System.out.println(c[m]);
    }

    時間複雜度O(n)時間

  5. 快速排序while

    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;
    }
相關文章
相關標籤/搜索