The title
Given an array of integers sorted in non-descending order, nums returns a new array of the squares of each number, also sorted in non-descending order.
Example 1: input: nums = [-4,-1,0,3,10] output: [0,1,9,16 100] description: after the square is squared, the array becomes [16,1,0,9, 9,100] after the sort, the array becomes [0,1,9,16,100] example 2: input: Nums = [-7,-3,2,3,11]Copy the code
Tip:
1 <= nums.length <= 104-104 <= nums[I] <= 104 NUMs have been sorted in non-descending order
Advanced:
Please design an algorithm with O(n) time complexity to solve this problem
Their thinking
Class Solution: def sortedSquares(self, nums: List[int]) -> List[int]: abs(x)) # print(nums) return [x*x for x in nums] if __name__ == '__main__': Nums = Solution().sortedsquares (NUMs) print(ret)Copy the code