[Leetcode] 977. 有序数组的平方

题目链接:https://leetcode-cn.com/problems/squares-of-a-sorted-array/
分析:
双指针。
Python

class Solution:
    def sortedSquares(self, A: List[int]) -> List[int]:
        if len(A) == 0:
            return []
        length = len(A) 
        l, r, i = 0, length-1, length-1
        res = [0]*length
        while l <= r:
            left = A[l]**2
            right = A[r]**2
            if left < right:
                res[i] = right
                r -= 1
            else:
                res[i] = left
                l += 1
            i -= 1
        return res
posted @ 2020-10-16 22:44  我的小叮当  阅读(101)  评论(0编辑  收藏  举报