題目
給定一個(gè)按非遞減順序排序的整數(shù)數(shù)組 A,返回每個(gè)數(shù)字的平方組成的新數(shù)組,要求也按非遞減順序排序。
示例 1:
輸入:[-4,-1,0,3,10]
輸出:[0,1,9,16,100]
示例 2:
輸入:[-7,-3,2,3,11]
輸出:[4,9,9,49,121]
提示:
1 <= A.length <= 10000
-10000 <= A[i] <= 10000
A 已按非遞減順序排序。
C++解法
class Solution {
public:
static bool comparator(int lhs, int rhs) {return lhs < rhs;}
vector<int> sortedSquares(vector<int>& A) {
for (int i = 0; i < A.size(); i ++) {
A[i] *= A[i];
}
sort(A.begin(), A.end(), comparator);
return A;
}
};
來(lái)源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/squares-of-a-sorted-array