1.暴力法:
class Solution(object):
def sortedSquares(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
for i in range(len(nums)):
nums[i]*=nums[i]
nums.sort()
return nums
2.双指针
class Solution(object):
def sortedSquares(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
l=len(nums)
res=[0]*l
ini_idx=0
last_idx=l-1
for i in range(l-1,-1,-1):
l_num=nums[last_idx]*nums[last_idx]
i_num=nums[ini_idx]*nums[ini_idx]
if l_num>i_num:
res[i]=l_num
last_idx-=1
else:
res[i]=i_num
ini_idx+=1
return res