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/ 目录 题目描述 题目大意 解题方法 库函数排序 桶排序 红黑树排序 归并排序 快速排序 ...
随机推荐
- Mysql忘记root密码怎么办?(已解决)
为了写这篇文档,假装一下忘记密码!!!! 首先我数据库是正常的,可以使用. root@localhost:~# mysql -uroot -p mysql> mysql> show dat ...
- == 与 equals 之区别
"=="和equals方法究竟有什么区别? (单独把一个东西说清楚,然后再说清楚另一个,这样,它们的区别自然就出来了,混在一起说,则很难说清楚) ==操作符专门用来比较两个变量的值 ...
- UPX压缩
什么是UPX UPX (the Ultimate Packer for eXecutables)是一款先进的可执行程序文件压缩器,压缩过的可执行文件体积缩小50%-70% ,这样减少了磁盘占用空间.网 ...
- php中普通方法和静态方法的区别以及抽象类和接口
实例化类产生对象.class fenbi{ //普通成员,属于对象 public $length = "10cm"; //静态成员,静态变量,属于类. public static ...
- MVC学习笔记---WebViewPage(nop等开源项目的@T)
http://www.cnblogs.com/gyche/p/5597491.html http://www.cnblogs.com/Alex80/p/5369042.html http://www. ...
- 内存泄漏(memory leak)和内存溢出
1. 什么是内存泄漏(memory leak)? 指由于疏忽或错误造成程序未能释放已经不再使用的内存的情况.内存泄漏并非指内存在物理上的消失,而是应用程序分配某段内存后,由于设计错误,失去了对该段内存 ...
- 【Luogu】P2489迷宫探险(概率DP)
题目链接 设f[i][j][k][l]是当前在(i,j),对陷阱的了解状态为k(0表示了解该陷阱为无危险,1表示了解该陷阱有危险,2不了解),l表示当前血,走出迷宫的概率 dfsDP即可. 注意随时更 ...
- 少年Pi的奇幻漂流
选择怀疑作为生活哲学就像选择静止作为交通方式. 的确,我们遇见的人可能改变我们,有时候改变如此深刻,在那之后我们成了完全不同的人,甚至我们的名字都不一样了. 声音会消失,但伤害却留了下来,像小便蒸 ...
- [hdu6437]Problem L. Videos
题目大意:有$n$个小时,有$m$个节目(每种节目都有类型$0/1$),有$k$个人,一个人连续看相同类型的节目会扣$w$快乐值. 每一种节目有都一个播放区间$[l,r]$.每个人同一时间只能看一个节 ...
- python使用openpyxl操作excel
def initExcel(): file_path = "test.xlsx" file = load_workbook(file_path) table = file[&quo ...