问题:c++不允许使用类型名。
#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
vector<int> sortedSquares(vector<int>& nums)
{
int l = 0;
int r = nums.size() - 1;
int pos = r;
vector<int> ans(pos + 1, 0);
while (l <= r)
{
if (abs(nums[r]) > abs(nums[l]))
{
ans[pos] = nums[r] * nums[r];
r--;
pos--;
}
else
{
ans[pos] = nums[l] * nums[l];
l++;
pos--;
}
}
return ans;
}
};
int main()
{
vector<int>nums = { -4, -1, 0, 3, 10 };
vector<int> v;
v.swap(Solution.sortedSquares(nums));
for (auto ans : v)
{
cout << ans << endl;
}
}