public static int[] sortedSquares(int[] nums) { //定义一个双指针 int left=0,right=nums.length-1; int[] res = new int[right+1]; //因为新数组需要排序 for (int i = nums.length-1; left <= right;i--) { //如果左边大 if (nums[left]*nums[left]>nums[right]*nums[right]){ res[i]=nums[left]*nums[left]; left++; }else { res[i]=nums[right]*nums[right]; right--; } } return res; }