注意要倒着来
public static int[] sortedSquares(int[] nums) {
int[] result = new int[nums.length];
int index = nums.length - 1;
int left = 0;
int right = nums.length - 1;
for (; left <= right; ) {
if (nums[left] * nums[left] < nums[right] * nums[right]) {
result[index] = nums[right] * nums[right];
right--;
} else {
result[index] = nums[left] * nums[left];
left++;
}
index--;
}
return result;
}
public static void main(String[] args) {
int[] nums={-4,-1,0,3,10};
sortedSquares(nums);
}