977. Squares of a Sorted Array
https://leetcode.com/problems/squares-of-a-sorted-array/
/*
* @lc app=leetcode id=977 lang=cpp
*
* [977] Squares of a Sorted Array
*/
// @lc code=start
class Solution {
public:
vector<int> sortedSquares(vector<int>& A) {
const int N = A.size();
vector<int> res(N, 0);
for (int i = 0, j = N - 1, k = N - 1; k >= 0; --k) {
if (abs(A[i]) < abs(A[j])) res[k] = A[j] * A[j--];
else res[k] = A[i] * A[i++];
}
return res;
}
};
// @lc code=end
Last updated