mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
add a new python version
This commit is contained in:
@ -178,6 +178,24 @@ class Solution:
|
||||
return sorted(x*x for x in nums)
|
||||
```
|
||||
|
||||
```Python
|
||||
(版本四) 双指针+ 反转列表
|
||||
class Solution:
|
||||
def sortedSquares(self, nums: List[int]) -> List[int]:
|
||||
#根据list的先进排序在先原则
|
||||
#将nums的平方按从大到小的顺序添加进新的list
|
||||
#最后反转list
|
||||
new_list = []
|
||||
left, right = 0 , len(nums) -1
|
||||
while left <= right:
|
||||
if abs(nums[left]) <= abs(nums[right]):
|
||||
new_list.append(nums[right] ** 2)
|
||||
right -= 1
|
||||
else:
|
||||
new_list.append(nums[left] ** 2)
|
||||
left += 1
|
||||
return new_list[::-1]
|
||||
|
||||
### Go:
|
||||
|
||||
```Go
|
||||
|
Reference in New Issue
Block a user