You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
func sortedSquares(a []int) []int {
n := len(a)
ans := make([]int, n)
i, j := 0, n-1
for pos := n - 1; pos >= 0; pos-- {
if v, w := a[i]*a[i], a[j]*a[j]; v > w {
ans[pos] = v
i++
} else {
ans[pos] = w
j--
}
}
return ans
}
给定一个按非递减顺序排序的整数数组 A,返回每个数字的平方组成的新数组,要求也按非递减顺序排序。
示例 1:
示例 2:
提示:
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/squares-of-a-sorted-array
The text was updated successfully, but these errors were encountered: