LeetCode 977 Squares of a Sorted Array (Python)

Posted by 小明MaxMing on December 15, 2020

题目

Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.

解题思路

使用双指针,从两端开始,每次将绝对值较大的数的平方倒序加到结果里

代码

class Solution:
    def sortedSquares(self, nums: List[int]) -> List[int]:
        l = len(nums)
        left, right = 0, l - 1
        res = [0] * l
        cur = l - 1
        while left <= right:
            if abs(nums[left]) > abs(nums[right]):
                res[cur] = nums[left] ** 2
                left += 1
            else:
                res[cur] = nums[right] ** 2
                right -= 1
            cur -= 1
        return res

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅