代码拉取完成,页面将自动刷新
// Time: O(n)
// Space: O(1)
class Solution {
public:
vector<int> sortTransformedArray(vector<int>& nums, int a, int b, int c) {
const auto f = [](int x, int a, int b, int c) {
return a * x * x + b * x + c;
};
vector<int> result;
if (nums.empty()) {
return result;
}
int left = 0, right = nums.size() - 1;
int d = a > 0 ? -1 : 1;
while (left <= right) {
if (d * f(nums[left], a, b, c) < d * f(nums[right], a, b, c)) {
result.emplace_back(f(nums[left++], a, b, c));
} else {
result.emplace_back(f(nums[right--], a, b, c));
}
}
if (d == -1) {
reverse(result.begin(), result.end());
}
return result;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。