LeetCode之有序数组的平方

执行用时 : 312 ms, 在Squares of a Sorted Array的C++提交中击败了8.00% 的用户

内存消耗 : 15.3 MB, 在Squares of a Sorted Array的C++提交中击败了100.00% 的用户

老夫写算法从来是暴力一把梭;其实这题吧,把每个平方存起来然后排个序就好了,也懒的想其他方法了。

内存少啊

class Solution {
public:
    vector<int> sortedSquares(vector<int>& A) {
        vector<int>ans;
        for( int i = 0 ; i < A.size() ; i++ ){
            ans.push_back( A[i] * A[i] );
        }
        sort( ans.begin() , ans.end() );
        return ans;
    }
};

猜你喜欢

转载自blog.csdn.net/wuhenglan/article/details/88319267