注意要倒着来
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);
}
博客提示内容要倒着来,但未明确具体对象。推测可能在信息技术操作中有倒序需求,如数据排序等。
3083

被折叠的 条评论
为什么被折叠?



