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


	[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

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

原文地址:https://www.cnblogs.com/zuotongbin/p/13829140.html

hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » [Leetcode] 977. 有序数组的平方