[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(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 = [-4, -2, 2, 4], a = 1, b = 3, c = 5, Result: [3, 9, 15, 33] nums = [-4, -2, 2, 4], a = -1, b = 3, c = 5 Result: [-23, -5, 1, 7]
Credits:
Special thanks to @elmirap for adding this problem and creating all test cases.
解法1:把每一个数带入方程计算结果,然后对结果排序。Time: O(nlogn),不是题目想要的
解法2:根据抛物线方程式的特点,a>0则抛物线开口朝上,两端的值比中间的大,a<0则抛物线开口朝下,则两端的值比中间的小,a=0时,则为直线方法,是单调递增或递减的。同时因为给定数组nums是有序的,所以才有O(n)的解法。
当a>0,两端的值比中间的大,从后边往中间填数,用两个指针分别指向数组的开头和结尾,比较两个数,将其中较大的数存入res,然后该指针向中间移,重复比较过程,直到把res都填满。
当a<0,两端的值比中间的小,从前边往中间填数,用两个指针分别指向数组的开头和结尾,比较两个数,将其中较小的数存入res,然后该指针向中间移,重复比较过程,直到把res都填满。
当a=0,函数是单调递增或递减的,那么从前往后填和从后往前填都可以,也可以将这种情况和a>0合并。
Java:
public class Solution {
private int calcu(int x, int a, int b, int c){
return a*x*x + b*x + c;
}
public int[] sortTransformedArray(int[] nums, int a, int b, int c) {
int index;
if(a > 0){
index = nums.length - 1 ;
} else {
index = 0;
}
int result[] = new int[nums.length];
int i = 0;
int j = nums.length - 1;
if(a > 0){
while(i <= j){
result[index--] = calcu(nums[i],a,b,c) > calcu(nums[j],a,b,c) ? calcu(nums[i++],a,b,c):calcu(nums[j--],a,b,c);
}
}else{
while(i <= j){
result[index++] = calcu(nums[i],a,b,c) < calcu(nums[j],a,b,c) ? calcu(nums[i++],a,b,c):calcu(nums[j--],a,b,c);
}
}
return result;
}
}
Python: wo
class Solution():
def sortTransformedArray(self, nums, a, b, c):
if not nums:
return []
res = [0] * len(nums)
i, j = 0, len(nums) - 1
index = len(nums) - 1 if a >= 0 else 0
while i <= j:
if a >= 0:
calc_i = self.calc(nums[i], a, b, c)
calc_j = self.calc(nums[j], a, b, c)
if calc_i >= calc_j:
res[index] = calc_i
i += 1
else:
res[index] = calc_j
j -= 1
index -= 1
else:
calc_i = self.calc(nums[i], a, b, c)
calc_j = self.calc(nums[j], a, b, c)
if calc_i <= calc_j:
res[index] = calc_i
i += 1
else:
res[index] = calc_j
j -= 1
index += 1 return res def calc(self, x, a, b, c):
return a * x * x + b * x + c if __name__ == '__main__':
print Solution().sortTransformedArray([], 1, 3, 5)
print Solution().sortTransformedArray([2], 1, 3, 5)
print Solution().sortTransformedArray([-4, -2, 2, 4], 1, 3, 5)
print Solution().sortTransformedArray([-4, -2, 2, 4], -1, 3, 5)
print Solution().sortTransformedArray([-4, -2, 2, 4], 0, 3, 5)
C++:
class Solution {
public:
vector<int> sortTransformedArray(vector<int>& nums, int a, int b, int c) {
int n = nums.size(), i = 0, j = n - 1;
vector<int> res(n);
int idx = a >= 0 ? n - 1 : 0;
while (i <= j) {
if (a >= 0) {
res[idx--] = cal(nums[i], a, b, c) >= cal(nums[j], a, b, c) ? cal(nums[i++], a, b, c) : cal(nums[j--], a, b, c);
} else {
res[idx++] = cal(nums[i], a, b, c) >= cal(nums[j], a, b, c) ? cal(nums[j--], a, b, c) : cal(nums[i++], a, b, c);
}
}
return res;
}
int cal(int x, int a, int b, int c) {
return a * x * x + b * x + c;
}
};
C++:
class Solution {
public:
vector<int> sortTransformedArray(vector<int>& nums, int a, int b, int c) {
if(nums.size() ==0) return {};
vector<int> result;
int left = 0, right = nums.size()-1;
auto func = [=](int x) { return a*x*x + b*x + c; };
while(left <= right)
{
int val1 = func(nums[left]), val2 = func(nums[right]);
if(a > 0) result.push_back(val1>=val2?val1:val2);
if(a > 0) val1>val2?left++:right--;
if(a <= 0) result.push_back(val1>=val2?val2:val1);
if(a <= 0) val1>val2?right--:left++;
}
if(a > 0) reverse(result.begin(), result.end());
return result;
}
};
All LeetCode Questions List 题目汇总
[LeetCode] 360. Sort Transformed Array 排序转换后的数组的更多相关文章
- LeetCode 360. Sort Transformed Array
原题链接在这里:https://leetcode.com/problems/sort-transformed-array/description/ 题目: Given a sorted array o ...
- 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: 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] 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] 75. Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- 【LeetCode】Find Minimum in Rotated Sorted Array 找到旋转后有序数组中的最小值
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4032570.html 原题: Suppose a sorted array is ...
- [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( ...
- 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( ...
随机推荐
- CentOS7.5下搭建的SVN实现删除权限控制和必须进行注释的提示操作
需求:上传到SVN服务器的项目文件如果被普通用户误删了,虽然能恢复,但是如果删除的文件比较多,注释的内容简单,恢复的时候需要一个个的保存到本地,然后再上传到服务器上,会很麻烦,可能还会出现提交代码版本 ...
- 在inux中安装redis的时候,会出现下面的这个异常
是因为没有安装c++的编译器 安装c++的编译器: yum -y install gcc-c++ 然后再使用命令执行make就可以了 ,如果你遇到这个错误以后,一定要先将redis的解压包删掉以后,再 ...
- CentOS 6.x 无法格式化大于16TB的ext4分区处理
CentOS 6.x 在格式化大于16TB的ext4分区时,会提示如下错误: mke2fs 1.41.12 (17-May-2010) mkfs.ext4: Size of device /dev/s ...
- eclipse 配置python环境 json 插件
windows->install new software add 配置python 环境: name:pydev(可随意写) url:http://pydev.org/updates/ (如果 ...
- asp.net解决大文件断点续传
以ASP.NET Core WebAPI 作后端 API ,用 Vue 构建前端页面,用 Axios 从前端访问后端 API ,包括文件的上传和下载. 准备文件上传的API #region 文件上传 ...
- JS全局变量是如何工作的?
JS全局变量是如何工作的? <script> const one = 1; var two = 2; </script> <script> // All scrip ...
- Angular2发送HTTP请求SpringBoot后台跨域问题解决
Angular通过http发送post请求至SpringBoot的Controller,由于同源策略的保护,遇到跨域问题: • 源(origin)就是协议(http).域名(localhost)和端口 ...
- 手把手教你用Python代码实现微信聊天机器人 -- Python wxpy
关注我,每天都有优质技术文章推送,工作,学习累了的时候放松一下自己. 本篇文章同步微信公众号 欢迎大家关注我的微信公众号:「醉翁猫咪」 来学习了,微信聊天机器人. 环境要求: Windows / Li ...
- 开机启动类似于Tencent Upd的弹窗解决方法
1.开机启动的程序,后台启动自动升级的exe,每次开机都弹出弹窗,一不小心就点错了,神烦. 解决方式:直接在windows系统 [ 本地安全策略>软件限制策略>其他规则 ] 里面把弹出的 ...
- 公司不用 Spring Boot,果断离职了!
面试问到离职原因,我想这是很多面试者的痛,包括我自己,曾经也被离职原因所坑过. 面试回答离职原因简直特么就是巨坑,我也因此在微信公众号 "Java技术栈" 写了这篇文章<过了 ...