1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# 977. Squares of a Sorted Array
"""
given an integer array 'nums' sorted in non-decreasing order, return an array
of the squares sorted in non-decreasing order.
"""
class Solution(object):
def sortedSquares(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
ans = [None for _ in nums]
left, right = 0, len(nums) - 1
for i in range(len(nums) - 1, -1, -1):
if abs(nums[left]) > abs(nums[right]):
ans[i] = nums[left] ** 2
left += 1
else:
ans[i] = nums[right] ** 2
right -= 1
return ans
if __name__ == "__main__":
obj = Solution()
print(obj.sortedSquares(nums=[-4, -1, 0, 3, 10]))
print(obj.sortedSquares(nums=[-7, -3, 2, 3, 11]))
|