链接:https://leetcode.cn/problems/squares-of-a-sorted-array/
思路:
双指针变种,两头向中间遍历,既然找不到最小值在哪,那就判断最大值,逆序插入
class Solution {
public int[] sortedSquares(int[] nums) {
int a[] = new int[nums.length];
int l = 0, r = nums.length - 1, k = nums.length - 1;
while(r >= l) {
int s1 = nums[l] * nums[l];
int s2 = nums[r] * nums[r];
if(s1 < s2) {
a[k --] = s2;
r --;
} else {
a[k --] = s1;
l ++;
}
}
return a;
}
}