本文共 519 字,大约阅读时间需要 1 分钟。
给定一个数组,要求将其平方后排序。代码如下:
import java.util.Arrays;public class Solution { /** * @param A: The array A. * @return: The array of the squares. */ public int[] SquareArray(int[] A) { // write your code here if (A == null || A.length == 0) { return A; } for (int i = 0; i < A.length; i++) { A[i] = A[i] * A[i]; } Arrays.sort(A); return A; }}
时间复杂度 O ( n log n ) O(n\log n) O(nlogn),空间 O ( 1 ) O(1) O(1)。
转载地址:http://uqds.baihongyu.com/