代码随想录 Day2
今日任务
977.有序数组的平方
209.长度最小的子数组
59.螺旋矩阵Ⅱ
语言:Java
977. 有序数组的平方
考点:双指针
链接:https://leetcode.cn/problems/squares-of-a-sorted-array/
注意事项:对于非递减排序的数组来说,平方后的最大值只能在两边取得,最小值在中间取得,在移动指针过程中left和right不要忘记调整
class Solution {
public int[] sortedSquares(int[] nums) {
int left = 0;
int right = nums.length - 1;
int[] result = new int[nums.length];
for(int i = nums.length - 1; i>= 0;){
if(nums[left] * nums[left] < nums[right] * nums[right]){
result[i--] = nums[right] * nums[right];
right--;
}
else if(nums[l