码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 977 Squares of a Sorted Array

时间:2019-08-11 13:22:40      阅读:57      评论:0      收藏:0      [点我收藏+]

标签:i+1   ret   pre   run   array   mem   sort   int   sage   

 

待优化

 

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

标签:i+1   ret   pre   run   array   mem   sort   int   sage   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!