LeetCode 977 Squares of a Sorted Array

 

待优化

 

// Runtime: 1157 ms, faster than 5.00%
// Memory Usage: 40.8 MB, less than 96.34%s
class Solution {
    public int[] sortedSquares(int[] A) {
        for (int i=0; i) {
            for (int j=i+1; j) {
                if (Math.abs(A[j])<Math.abs(A[i])) {
                    int temp = A[i];
                    A[i] = A[j];
                    A[j] = temp;
                }
            }
            A[i] = A[i]*A[i];
        }
        return A;
    }
}


// Runtime: 286 ms, faster than 5.00%
// Memory Usage: 41.3 MB, less than 95.73%
public int[] sortedSquares2(int[] A) {

    for (int i=0; i) {
        A[i] = A[i]*A[i];
    }

    for (int i=0; i) {
        for (int j=i+1; j) {
            if (A[j]<A[i]) {
                int temp = A[i];
                A[i] = A[j];
                A[j] = temp;
            }
        }
    }
    return A;
}

 

你可能感兴趣的:(LeetCode 977 Squares of a Sorted Array)