Squares of a Sorted Array
Easy · two pointers
A list of whole numbers nums is given in non-decreasing order (smallest first). Some of the numbers may be negative, some zero and some positive.
Square every number, then return a new list holding all the squares in non-decreasing order. The result has the same length as nums, and equal squares appear as many times as they occur.
Examples
Example 1
Input: nums = [-4, -1, 0, 3, 10]
Output: [0, 1, 9, 16, 100]
The squares are 16, 1, 0, 9 and 100. Put in order, they read 0, 1, 9, 16, 100.
Example 2
Input: nums = [-7, -3, 2, 3, 11]
Output: [4, 9, 9, 49, 121]
-3 and 3 both square to 9, so 9 appears twice.
Example 3
Input: nums = [5]
Output: [25]
Constraints
- 1 <= len(nums) <= 10^5
- -10^4 <= nums[i] <= 10^4
- nums is sorted in non-decreasing order.