Problem: 977. 有序数组的平方
👨🏫 灵神题解
class Solution {
public int[] sortedSquares(int[] nums) {
int n = nums.length;
int [] ans = new int[n];
int p = n-1;
int i = 0;
int j = n-1;
while(p >= 0){
int x = nums[i] * nums[i];
int y = nums[j] * nums[j];
if(x >= y){
ans[p] = x;
i++;
}else{
ans[p] = y;
j--;
}
p--;
}
return ans;
}
}