← 返回 uber 的题目列表Squares of a Sorted Array (with constraints discussion)
类型:online_judge
Problem: Squares of a Sorted Array
Given an integer array nums sorted in non-decreasing order (may contain negative numbers), return a new array ans where ans[i] = nums[i]^2, and ans is also sorted in non-decreasing order.
Requirements
Time complexity: O(n)
Space complexity: you may use an output array (O(n)).
Input/Output
Input: one line of integers representing nums
Output: one line of integers representing ans
Constraints
1 <= n <= 10^5
-10^4 <= nums[i] <= 10^4
nums is already sorted in non-decreasing order
Examples
nums = [-4,-1,0,3,10] -> [0,1,9,16,100]
nums = [-7,-3,2,3,11] -> [4,9,9,49,121]
Test Cases (5)
Input: -4 -1 0 3 10 Output: 0 1 9 16 100
Input: -7 -3 2 3 11 Output: 4 9 9 49 121
Input: 0 Output: 0
Input: -5 -4 -3 -2 -1 Output: 1 4 9 16 25
Input: 1 2 3 4 5 Output: 1 4 9 16 25
Example
Input
-4 -1 0 3 10
Output
0 1 9 16 100