(题解)《算法零基础100讲》(第37讲) 排序进阶 - 快速排序

1、最小时间差(leetcode539

计算出分钟数,然后双重循环计算最小值,注意最小值那里的一个坑,就是也要算一下减去24小时的值。

class Solution {
    public int findMinDifference(List<String> timePoints) {
        Iterator<String> iterator = timePoints.iterator();
        int []arr =new  int[timePoints.size()];
        int i = 0 ;
        while(iterator.hasNext())
        {
            char[] chars = iterator.next().toCharArray();
            if((((chars[0] - '0') * 10 + (chars[1] - '0')) * 60 + (((chars[3] - '0') * 10 + (chars[4] - '0')))) == 0)
            {
                arr[i] = 24 * 60;
            }
            else
            {
                arr[i] =((chars[0] - '0') * 10 + (chars[1] - '0')) * 60 + (((chars[3] - '0') * 10 + (chars[4] - '0')));
            }
            i ++;
            
        }
        int min = 24 * 60 + 1;
        for(int j = 0 ; j < arr.length ; j ++)
        {
            for(int k = j + 1;k < arr.length ; k ++)
            {
                int time = Math.min(Math.abs(arr[j] - arr[k]), Math.abs(Math.abs(arr[j] - arr[k]) - 24 * 60));
                min = Math.min(time,min);
            }
        }
        return min;
    }
}

2、有序数组的平方(leetcode977

水题,平方后快排就可以了

class Solution {
    public int[] sortedSquares(int[] nums) {
        int len = nums.length;
        for(int i =  0 ; i < len; i ++)
        {
            nums[i] = nums[i] * nums[i];
        }
        quick_sort(nums, 0 , len - 1);
        return nums;
    }
    public void quick_sort(int []q, int l, int r)
    {
        if(l >= r)return;
        int mid = q[(l + r) / 2];
        int i = l - 1 , j = r + 1;
        while( i < j)
        {
            do i++;while(q[i] < mid);
            do j--;while(q[j] > mid);
            if(i < j)
            {
                int n = q[i];
                q[i] = q[j];
                q[j] = n;
            }
        }
        quick_sort(q , l , j);
        quick_sort(q, j + 1, r);
    }
}

3、优势洗牌(leetcode870

首先将数组A排序,然后利用优先队列构建大根堆,大根堆中记录数组B的下标和数值,根据数值进行建堆,每次弹出大根堆的堆顶,也就是最大值,创建 l = 0,r = A.length - 1 表示数组A的双指针,先是和数组A中 A[r] 比较,如果小于该最大值,就将A[r]放入结果的对应位置中,接着r – ,否则将A[l] 放入对应位置中,接着l ++;

class Solution {
public int[] advantageCount(int[] nums1, int[] nums2) {
        int len1 = nums1.length ,len2 = nums2.length;
        quick_sort(nums1 , 0 , len1 -1 );
        PriorityQueue<int[]> p = new PriorityQueue<>(new Comparator<int[]>() {
            @Override
            public int compare(int[] ints, int[] t1) {
                return t1[1] - ints[1];
            }
        });
        for(int i = 0 ; i < len2 ; i ++)
        {
            p.offer(new int[]{i,nums2[i]});
        }
        int []res = new int[len1];
        int l = 0,r = len1 - 1;
        while(!p.isEmpty())
        {
            int []t = p.poll();
            if(t[1] < nums1[r])res[t[0]] = nums1[r--];
            else res[t[0]] = nums1[l ++];
        }
        return res;

    }
    public void quick_sort(int []nums , int l, int r)
    {
        if(l >= r)return ;
        int mid = nums[(l + r) / 2 ];
        int i = l - 1 , j = r + 1;
        while( i  <  j)
        {
            do i ++ ; while(nums[i] < mid);
            do j -- ; while(nums[j] > mid);
            if(i < j)
            {
                int n = nums[i] ;
                nums[i] = nums[j];
                nums[j] = n;
            }
        }
        quick_sort(nums, l, j);
        quick_sort(nums, j + 1 , r);
    }
}

4、救生艇(leetcode881

水题,先排序,然后双指针遍历。

class Solution {
   public int numRescueBoats(int[] people, int limit) {
        Arrays.sort(people);
        int len = people.length;
        int l = 0 , r = len - 1 , count = 0;
        while(l <= r)
        {
            if(limit - people[r] >= people[l])
            {
                l ++;
                r --;
            }
            else
            {
                r --;
            }
            count ++;
        }
        return count;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值