LeetCode 977 Squares of a Sorted Array

Posted stoneBlog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

 

以上是关于LeetCode 977 Squares of a Sorted Array的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 977. Squares of a Sorted Array

Leetcode_easy977. Squares of a Sorted Array

LeetCode 977 Squares of a Sorted Array

LeetCode 977 Squares of a Sorted Array 解题报告

LeetCode --- 977. Squares of a Sorted Array 解题报告

Leetcode 977. Squares of a Sorted Array