Sort Transformed Array -- LeetCode
Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f(x) = ax2 + bx + c to each element x in the array.
The returned array must be in sorted order.
Expected time complexity: O(n)
Example:
nums = [-, -, , ], a = , b = , c = , Result: [, , , ] nums = [-, -, , ], a = -, b = , c = Result: [-, -, , ]
思路:因为是排好序的数组,我们用两个指针从数组两边向中间计算答案。根据每次计算的函数值的大小决定移动哪个指针。复杂度O(N)。
class Solution {
public:
int f(int x, int a, int b, int c) {
return a * x * x + b * x + c;
}
vector<int> sortTransformedArray(vector<int>& nums, int a, int b, int c) {
int len = nums.size();
vector<int> res(len);
int left = , right = nums.size() - , count = ;
while (left <= right) {
int leftRes = f(nums[left], a, b, c);
int rightRes = f(nums[right], a, b, c);
bool goLeft = (a >= && leftRes >= rightRes) || (a < && leftRes <= rightRes);
int curPos = (a >= ? len - - count : count);
if (goLeft) {
res[curPos] = leftRes;
left++;
} else {
res[curPos] = rightRes;
right--;
}
count++;
}
return res;
}
};
Sort Transformed Array -- LeetCode的更多相关文章
- Leetcode: Sort Transformed Array
Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...
- [LeetCode] Sort Transformed Array 变换数组排序
Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...
- LeetCode 360. Sort Transformed Array
原题链接在这里:https://leetcode.com/problems/sort-transformed-array/description/ 题目: Given a sorted array o ...
- [LeetCode] 360. Sort Transformed Array 排序转换后的数组
Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...
- Sort Transformed Array
Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...
- 360. Sort Transformed Array二元一次方程返回大数序列
[抄题]: Given a sorted array of integers nums and integer values a, b and c. Apply a quadratic functio ...
- 360. Sort Transformed Array
一元二次方程...仿佛回到了初中. 主要看a的情况来分情况讨论: =0,一次函数,根据b的正负单调递增递减就行了. <0,凸状..从nums[]左右两边开始往中间一边比较一边 从右往左 放: 0 ...
- [LeetCode] 912. Sort an Array 数组排序
Given an array of integers nums, sort the array in ascending order. Example 1: Input: [5,2,3,1] Outp ...
- 【LeetCode】912. Sort an Array 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数排序 桶排序 红黑树排序 归并排序 快速排序 ...
随机推荐
- OZ customize windows iamge
1.之前通过修改auto文件和windows.py文件可实现自动安装OS,但是disk bus只能以IDE模式启动,进入系统自动下载运行bat文件,安装cloudbase-init和qga,及清理日志 ...
- OpenCV实现张正友相机标定源代码
本源代码基于VC++和opencv Opencv2.4.13.6版本开发,实现张正友相机标定源代码,资源包括完整源代码和12张棋盘图片,完美运行.Opencv2.4.13.6安装包下载地址:http: ...
- php利用PHPExcel类导出导入Excel用法
PHPExcel类是php一个excel表格处理插件了,下面我来给大家介绍利用PHPExcel类来导入与导出excel表格的应用方法,有需要了解的朋友不防参考参考(PHPExcel自己百度下载这里不介 ...
- 第一次软件工程作业补充plus
一.代码的coding地址:coding地址. 二.<构建之法>读后问题以及感言(补充): 1.对于7.3MSF团队模型,7.2.6保持敏捷,预期和适应变化,中的"我们是预期变化 ...
- 想玩API,这些套路我来告诉你!
小伙伴是不是时常听说各种api接口的问题呢,可能许多人第一感觉:那是什么个玩意儿,那么多人回去研究它,今天思梦PHP小编就来为你揭开他的神秘的面纱,先看一下百度百科上面的官方的解释: 其实说白了就是为 ...
- 有许多部分没有在cgroup中显示啊,current/high/low/min等等
没看见current/high/low/min 在cgroup中的显示内容
- 网站前后台分离 图片 flash 视频 等文件的共享问题
在网上找了,没有说到点子上的,不详细 问了有经验的同事,要建立 文件服务器,就是一个IIS 下的新网站,网站是共享图片 文件使用的专用网站 后台上传的图片保存在 文件服务器即 文件共享专用的网站目录地 ...
- icon fonts generator & svg
icon fonts generator https://icomoon.io/app/#/select https://icomoon.io/ http://fontastic.me/ http:/ ...
- 整数拆分 [dp+多项式插值]
题意 $1 \leq n \leq 10^{18}$ $2 \leq m \leq 10^{18}$ $1 \leq k \leq 20$ 思路 n,m较小 首先考虑朴素的$k=1$问题: $f[i] ...
- 洛谷 P3168 [CQOI2015]任务查询系统 解题报告
P3168 [CQOI2015]任务查询系统 题目描述 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分. 超级计算机中的任务用三元组\((S_i,E_i,P_i) ...