leetcode977. 有序数组的平方 + 排序算法

本文介绍了如何使用双指针优化解决LeetCode上的'排序数组平方'问题,展示了多种排序算法如冒泡、选择、插入和希尔排序的实现,并重点讲解了双指针法在时间复杂度上的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

继续练习双指针,题目链接:

https://leetcode-cn.com/problems/squares-of-a-sorted-array/submissions/

先来个暴力求解,遍历平方+调用排序算法

class Solution {
    public int[] sortedSquares(int[] nums) {
        int[] ans = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            ans[i] = nums[i] * nums[i];
        }
        Arrays.sort(ans);
        return ans;
    }
}

接下来就是风骚的双指针了

class Solution {
    public int[] sortedSquares(int[] nums) {
        int[] ans = new int[nums.length];
        int i = 0, j = nums.length - 1;
        int flag = nums.length - 1;
        //左右指针向中间收拢,大的放入ans数组,flag--
        while (i <= j) {
            if(nums[i] * nums[i] > nums[j] * nums[j]) {
                ans[flag] = nums[i] * nums[i];
                i++;
            } else {
                ans[flag] = nums[j] * nums[j];
                j--;
            }
            flag--;
        }
        return ans;
    }
}

while的终止条件也可以是flag >= 0,一个意思。


趁机复习排序算法!

1.Bubble Sort

class Solution {
    public int[] sortedSquares(int[] nums){
        int[] ans = new int[nums.length];
        for (int i = 0; i < nums.length; i++){
            ans[i] = nums[i] * nums[i];
        }
        for (int i = 0; i < nums.length; i++){
            for(int j = 0; j < nums.length - 1 - i;j++){
                if(ans[j] > ans[j + 1]){
                    int temp = ans[j + 1];
                    ans[j + 1] = ans[j];
                    ans[j] = temp;
                }
            }
        }
        return ans;
    }
}

2.Selection Sort​​​​​​​

class Solution {
    public int[] sortedSquares(int[] nums){
        int[] ans = new int[nums.length];
        int temp,min;
        for (int i = 0; i < nums.length; i++){
            ans[i] = nums[i] * nums[i];
        }
        for (int i = 0; i < nums.length - 1; i++){
            min = i;
            for(int j = i + 1; j < nums.length; j++){
                if(ans[j] < ans[min]){
                    min = j;
                }
            }
            temp = ans[i];
            ans[i] = ans[min];
            ans[min] = temp;
        }
        return ans;
    }
}

3.Insertion Sort

class Solution {
    public int[] sortedSquares(int[] nums){
        int[] ans = new int[nums.length];
        int pre,cur;
        for (int i = 0; i < nums.length; i++){
            ans[i] = nums[i] * nums[i];
        }
        for (int i = 0; i < nums.length - 1; i++){
            pre = i;
            cur = ans[i + 1];
            while(pre >= 0 && ans[pre] > cur){
                ans[pre + 1] = ans[pre];
                pre--;
            }
            ans[pre + 1] = cur;
        }
        return ans;
    }
}

4.Shell Sort

class Solution {
    public int[] sortedSquares(int[] nums){
        int[] ans = new int[nums.length];
        int len = nums.length;
        for (int i = 0; i < nums.length; i++){
            ans[i] = nums[i] * nums[i];
        }
        for(int gap = len / 2; gap > 0; gap = gap / 2){
            for(int i = gap; i < len; i++){
                int j = i;
                int current = ans[i];
                while(j - gap >= 0 && current < ans[j - gap]){
                    ans[j] = ans[j - gap];
                    j = j - gap;
                }
                ans[j] = current;
            }
        }
        //不得不说,这个牛逼
        return ans;
    }
}

5.Merge Sort

class Solution {
    public int[] sortedSquares(int[] nums){
        int[] ans = new int[nums.length];
        int len = nums.length;
        for (int i = 0; i < nums.length; i++){
            ans[i] = nums[i] * nums[i];
        }
        sort(ans,0,len-1);
        return ans;
    }
    public int[] sort(int[] a, int left, int right){
        int mid = (left + right) / 2;
        if(left < right){
            sort(a, left, mid);
            sort(a, mid + 1, right);
            merge(a, left, right, mid);
        }
        return a;
    }
    public void merge(int[] a,int left, int right,int mid){
        int[] temp = new int[right - left + 1];
        int i = left;
        int j = mid + 1;
        int k = 0;
        while(i <= mid && j <= right){
            if(a[i] < a[j]){
                temp[k++] = a[i++];
            }else{
                temp[k++] = a[j++];
            }
        }
        while(i <= mid){
            temp[k++] = a[i++];
        }
        while(j <= right){
            temp[k++] = a[j++];
        }
        for(int x = 0; x < temp.length; x++){
            a[x+left] = temp[x];
        }

    }
}

剩下几个排序下次再写,下次一定

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值