977.有序数组的平方Go排序法

This commit is contained in:
markwang
2024-04-08 17:12:03 +08:00
parent 872f0deec1
commit c5a6c3e410

View File

@ -181,6 +181,17 @@ class Solution:
### Go ### Go
```Go ```Go
// 排序法
func sortedSquares(nums []int) []int {
for i, val := range nums {
nums[i] *= val
}
sort.Ints(nums)
return nums
}
```
```Go
// 双指针法
func sortedSquares(nums []int) []int { func sortedSquares(nums []int) []int {
n := len(nums) n := len(nums)
i, j, k := 0, n-1, n-1 i, j, k := 0, n-1, n-1