原题目:https://leetcode-cn.com/problems/squares-of-a-sorted-array/
思路:
使用双指针,从后向前加元素。
代码:
class Solution {
public:
vector<int> sortedSquares(vector<int>& A) {
int i=0,j=A.size()-1,index = A.size()-1;
vector<int> ans(A.size());
while(i <= j){
if(abs(A[i]) < abs(A[j])){
ans[index--] = A[j]*A[j];--j;
}
else{
ans[index--] = A[i]*A[i];++i;
}
}
return ans;
}
};