《LeetCode零基础指南》(第六讲) C排序API

1.排序数组

/**
堆排序的思想就是先将待排序的序列建成大根堆,使得每个父节点的元素大于等于它的子节点。此时整个序列最大值即为堆顶元素,我们将其与末尾元素交换,使末尾元素为最大值,然后再调整堆顶元素使得剩下的 n-1 个元素仍为大根堆,再重复执行以上操作我们即能得到一个有序的序列。

 */

class Solution {
    public int[] sortArray(int[] nums) {
        heapSort(nums);
        return nums;
    }

    public void heapSort(int[] nums) {
        int len = nums.length - 1;
        buildMaxHeap(nums, len);
        for (int i = len; i >= 1; --i) {
            swap(nums, i, 0);
            len -= 1;
            maxHeapify(nums, 0, len);
        }
    }

    public void buildMaxHeap(int[] nums, int len) {
        for (int i = len / 2; i >= 0; --i) {
            maxHeapify(nums, i, len);
        }
    }

    public void maxHeapify(int[] nums, int i, int len) {
        for (; (i << 1) + 1 <= len;) {
            int lson = (i << 1) + 1;
            int rson = (i << 1) + 2;
            int large;
            if (lson <= len && nums[lson] > nums[i]) {
                large = lson;
            } else {
                large = i;
            }
            if (rson <= len && nums[rson] > nums[large]) {
                large = rson;
            }
            if (large != i) {
                swap(nums, i, large);
                i = large;
            } else {
                break;
            }
        }
    }

    private void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}

2.多数元素

/**给定一个大小为 n 的数组,找到其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。
你可以假设数组是非空的,并且给定的数组总是存在多数元素*/
class Solution {
    public int majorityElement(int[] nums) {
        Arrays.sort(nums);
        return nums[nums.length / 2];//快速排序
    }
}

3.存在重复元素

/**
给定一个整数数组,判断是否存在重复元素。
如果存在一值在数组中出现至少两次,函数返回 true 。如果数组中每个元素都不相同,则返回 false 。
 */

 /**
 题解:
 (1)对数字从小到大排序之后,数组的重复元素一定出现在相邻位置中。
 (2)因此,我们可以扫描已排序的数组,每次判断相邻的两个元素是否相等,如果相等则说明存在重复的元素。
  */
class Solution {
    public boolean containsDuplicate(int[] nums) {
        Arrays.sort(nums);
        for(int i = 0; i < nums.length -1; i++){
            if(nums[i] == nums[i + 1]){
                return true;
            }
        }
        return false;
    }
}

4.最大间距

/*给定一个无序的数组,找出数组在排序之后,相邻元素之间最大的差值。如果数组元素个数小于 2,则返回 0。 */
class Solution {//
    public int maximumGap(int[] nums) {
        Arrays.sort(nums);
        if(nums.length < 2){
            return 0;
        }
        int res = 0;
        int ans = 0;
        for(int i = 0; i < nums.length - 1; i++){
            ans = nums[i+ 1] - nums[i];
            res = Math.max(res,ans);
        }

        return res;
    }
}

5.按奇偶排序数组

/*
1.给定一个非负整数数组 A,返回一个数组,在该数组中, A 的所有偶数元素之后跟着所有奇数元素。
2.你可以返回满足此条件的任何数组作为答案*/
class Solution {
    public int[] sortArrayByParity(int[] nums) {
        int i = 0;
        int j = nums.length - 1;
        while(i < j){
            while(i < j && nums[i] % 2 == 0){
                i++;
            }
            while(j > i && nums[j] % 2 != 0){
                j--;
            }
            int res = nums[i];
            nums[i] = nums[j];
            nums[j] = res;
        }
        return nums;
    }
}

6.最小时间差

/**给定一个 24 小时制(小时:分钟 "HH:MM")的时间列表,找出列表中任意两个时间的最小时间差并以分钟数表示。 */
class Solution {
    public int findMinDifference(List<String> timePoints) {
        int[] arr = new int[timePoints.size()];
        int cur = 0;
        for (int i = 0; i < timePoints.size(); i++) {
            String temp = timePoints.get(i);
            int time = 0;
            time = temp.charAt(0) * 10 + temp.charAt(1);
            time = time * 60 + temp.charAt(3) * 10 + temp.charAt(4);
            arr[cur++] = time;
        }
        Arrays.sort(arr);
        int ans = 24 * 60;
        for (int i = 0; i < arr.length - 1; i++) {
            ans = Math.min(ans, arr[i + 1] - arr[i]);
        }
        return Math.min(ans, arr[0] + 1440 - arr[arr.length - 1]);
    }
}

7.三角形的最大周长

/*
1.给定由一些正数(代表长度)组成的数组 A,返回由其中三个长度组成的、面积不为零的三角形的最大周长
2.如果不能形成任何面积不为零的三角形,返回 0*/
class Solution {
    public int largestPerimeter(int[] nums) {
        Arrays.sort(nums);//排序
        if(nums == null || nums.length < 3){
            return 0;
        }
        for(int i = nums.length - 1; i >= 2; i--){
            if(nums[i-1] + nums[i -2] > nums[i]){
                return nums[i -1] + nums[i -2] + nums[i];
            }
        }
        return 0;
    }
}

8.救生艇

/*
1.第 i 个人的体重为 people[i],每艘船可以承载的最大重量为 limit。
2.每艘船最多可同时载两人,但条件是这些人的重量之和最多为 limit。
3.返回载到每一个人所需的最小船数。(保证每个人都能被船载)*/
class Solution {//贪心
    public int numRescueBoats(int[] people, int limit) {
        int count = 0;
        int light = 0;
        int heavy = people.length - 1;
        Arrays.sort(people);
        while(light <= heavy){//试比较重量不超过limmit下,可以载重的最大可能
            if(people[light] + people[heavy] <= limit){
                light++;
            }
            heavy--;//每增加一人船的可载重减少1
            count++;
        }
        return count;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值