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<A.length; i++) {
            for (int j=i+1; j<A.length; 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.length; i++) {
        A[i] = A[i]*A[i];
    }

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

 

posted @ 2019-08-11 11:19  stoneBlog  阅读(96)  评论(0编辑  收藏  举报