class Solution {
public int[] sortedSquares(int[] nums) {
int left = 0;
int right = nums.length - 1;
int []squares = new int[nums.length];
int index = nums.length - 1;
while(left <= right) {
if(nums[left] * nums[left] < nums[right] * nums[right]) {
squares[index --] = nums[right] * nums[right];
right --;
} else {
squares[index --] = nums[left] * nums[left];
left ++;
}
}
return squares;
}
}
输入的数组本身是非递减的,则平方最大值一定在两端,使用双指针从两端往中间遍历即可依次得到平方最大值