package question;import java.util.Arrays;/*@author: Roderland@create: 2020年09月23日---20:28*/public class Question977 {public int[] sortedSquares(int[] A) {for (int i = 0; i < A.length; i++) {A[i] = A[i] * A[i];}Arrays.sort(A);return A;}public static void main(String[] args) {//[-4,-1,0,3,10]int[] a = new int[]{-2, 0};int[] ints = new Question977().sortedSquares2(a);for (int i : ints) {System.out.println(i);}}public int[] sortedSquares2(int[] A) {if (A.length == 1) return new int[]{A[0] * A[0]};int[] a = new int[A.length];int i = 0;while (A[i] < 0) i++;for (int j = 0; j < A.length; j++) A[j] = A[j] * A[j];int left = i - 1;int right = i;int j = 0;while (left >= 0 && right < A.length) {if (A[left] < A[right]) a[j++] = A[left--];else a[j++] = A[right++];}while (left >= 0) a[j++] = A[left--];while (right < A.length) a[j++] = A[right++];return a;}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。