小黑朝图leetcode之旅:977. 有序数组的平方

小黑最简做法

class Solution:
    def sortedSquares(self, nums: List[int]) -> List[int]:
        nums = [num*num for num in nums]
        return sorted(nums)

在这里插入图片描述

小黑最简代码java版

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

在这里插入图片描述

双指针1

class Solution:
    def sortedSquares(self, nums: List[int]) -> List[int]:
        # 声明分界线变量
        negitive_end = -1
        positive_start = -1
        n = len(nums)
        for num in nums:
            if num < 0:
                negitive_end += 1
            else:
                break
        # 正数的开始项
        positive_start = negitive_end + 1
        # 进行双指针遍历
        i = negitive_end
        j = positive_start
        ans = []
        while i >= 0 or j < n:
            if i < 0:
                ans.append(nums[j]*nums[j])
                j += 1
            elif j >= n:
                ans.append(nums[i]*nums[i])
                i -= 1
            elif nums[j]*nums[j] > nums[i]*nums[i]:
                ans.append(nums[i]*nums[i])
                i -= 1
            else:
                ans.append(nums[j]*nums[j])
                j += 1
        return ans

在这里插入图片描述

双指针1 java

class Solution {
    public int[] sortedSquares(int[] nums) {
        int n = nums.length;
        int[] ans = new int[n];
        int negative_end = -1;
        int positive_start = -1;
        for(int i = 0;i < n;i++){
            if(nums[i] < 0){
                negative_end++;
            }else{
                break;
            }
        }
        positive_start = negative_end + 1;
        int i = negative_end;
        int j = positive_start;
        int t = 0;
        while(i >= 0 || j < n){
            if(i < 0){
                ans[t] = nums[j]*nums[j];
                j++;
            }else if(j >= n){
                ans[t] = nums[i]*nums[i];
                i--;
            }else if(nums[i]*nums[i] < nums[j]*nums[j]){
                ans[t] = nums[i]*nums[i];
                i--;
            }else{
                ans[t] = nums[j]*nums[j];
                j++;
            }
            t++;
        }
        return ans;
    }
}

在这里插入图片描述

双指针2

class Solution:
    def sortedSquares(self, nums: List[int]) -> List[int]:
        n = len(nums)
        i = 0
        ans = [0] * n
        i,j,pos = 0,n-1,n-1
        while i < j:
            if nums[i]*nums[i] < nums[j]*nums[j]:
                ans[pos] = nums[j]*nums[j]
                j -= 1
            else:
                ans[pos] = nums[i]*nums[i]
                i += 1
            pos -= 1
        return ans

在这里插入图片描述

双指针2 java

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

在这里插入图片描述

小黑生活

请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值