LeetCode 977 Squares of a Sorted Array 解题报告

题目要求

Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.

题目分析及思路

题目给出一个整数数组,已经按照非减顺序排列好了。要求返回一个数组,包含的元素是已知数组元素的平方,且按照非减顺序排列。可以使用列表的sort()函数进行排序。

python代码​

class Solution:

    def sortedSquares(self, A):

        """

        :type A: List[int]

        :rtype: List[int]

        """

        B = list()

        for i in A:

            B.append(i**2)

        B.sort()

        return B

猜你喜欢

转载自www.cnblogs.com/yao1996/p/10317962.html