class Solution {
public int[] sortedSquares(int[] nums) {
//1.定义左右边界
int left = 0;
int right = nums.length - 1;
//2.创建新数组
int[] res = new int[nums.length];
int index = nums.length - 1;
//3.判定循环条件,即左右俩数相乘的平方进行比较
//注意最后一次是需要left = right的哦
while (left <= right) {
if (nums[left] * nums[left] < nums[right] * nums[right]) {
res[index] = nums[right] * nums[right];
right --;
index --;
} else {
res[index] = nums[left] * nums[left];
left ++;
index --;
}
}
return res;
}
}
07-14
174
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-26
262
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
02-26
269
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-14
698
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)