算法之双指针法(一)

   双指针法在很多场景及算法中都有使用
   主要应用的算法题如下:
  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)

  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)

  4. 两个有序数组合并且去重合成新数组

    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. 快速排序

    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;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值