Two Pointers
1 min
Two Pointers
//Leetcode977
class Solution {
public:
vector<int> sortedSquares(vector<int>& nums) {
for (int i=0;i<nums.size();i++)
{
nums[i]=nums[i]*nums[i];
}
sort(nums.begin(),nums.end());
return nums;
}};How would you write this problem using two pointers?
Merge sort?
class Solution {
public:
vector<int> sortedSquares(vector<int>& A) {
vector<int> res(A.size());
int l = 0, r = A.size() - 1;
for (int k = A.size() - 1; k >= 0; k--) {
if (abs(A[r]) > abs(A[l])) res[k] = A[r] * A[r--];
else res[k] = A[l] * A[l++];
}
return res;
}
};
Since it’s sorted in order, compare the absolute value of the first number with the absolute value of the last number, then put the larger one at the end.
Remember to create a new vector.