977.有序数组的平方
package com.leetcode.array;
public class OrderArraySquareSort {
public static void main(String[] args) {
int[] nums = {-4,-1,0,3,10};
int[] squ = sortArraySqu(nums);
for (int i = 0; i < squ.length; i++) {
System.out.println(squ[i]);
}
}
public static int[] sortArraySqu(int[] nums){
int len = nums.length;
int[] nums2 = new int[len];
int left = 0;
int right = nums.length - 1;
while (right >= left){
int leftSqu = nums[left] * nums[left];
int rightSqu = nums[right] * nums[right];
if(leftSqu > rightSqu){
nums2[len - 1] = leftSqu;
left++;
}
else {
nums2[len - 1] = rightSqu;
right--;
}
len--;
}
return nums2;
}
}