LeetCode 977 Squares of a Sorted Array

时间:2019-08-11 12:11:48   收藏:0   阅读:85

 

待优化

 

// 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;
}

 

原文:https://www.cnblogs.com/stone94/p/11334331.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!