Given a sorted array of integers nums and integer values ab 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 排序转换后的数组的更多相关文章

  1. LeetCode 360. Sort Transformed Array

    原题链接在这里:https://leetcode.com/problems/sort-transformed-array/description/ 题目: Given a sorted array o ...

  2. 360. Sort Transformed Array二元一次方程返回大数序列

    [抄题]: Given a sorted array of integers nums and integer values a, b and c. Apply a quadratic functio ...

  3. 360. Sort Transformed Array

    一元二次方程...仿佛回到了初中. 主要看a的情况来分情况讨论: =0,一次函数,根据b的正负单调递增递减就行了. <0,凸状..从nums[]左右两边开始往中间一边比较一边 从右往左 放: 0 ...

  4. 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( ...

  5. [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 ...

  6. [LeetCode] 75. Sort Colors 颜色排序

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  7. 【LeetCode】Find Minimum in Rotated Sorted Array 找到旋转后有序数组中的最小值

     本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4032570.html 原题: Suppose a sorted array is ...

  8. [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( ...

  9. 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( ...

随机推荐

  1. python的readline() 和readlines()

    .readline() 和 .readlines() 之间的差异是后者一次读取整个文件,象 .read() 一样..readlines() 自动将文件内容分析成一个行的列表,该列表可以由 Python ...

  2. redis提权

    介绍:Redis是一个开源的使用ANSI C语言编写.遵守BSD协议.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.它通常被称为数据结构服务器,因为值(va ...

  3. 是Mscoreei.dll的正确版本吗?

    在安装.NET 4.0或更高版本之后,您可能会注意到.NET进程有点不寻常.下面是用.NET 2.0编译器编译的简单“Hello World”可执行文件的加载模块的部分列表. 开始-结束模块名称 60 ...

  4. 玩家属性同步优化-脏数据标记(位运算、数组、stl之bitset)

    把大神的帖子中一部分摘抄出来,结合自己写的位运算代码和循环代码(数组遍历)进行性能测试分析并给出结果. 摘自: https://www.gameres.com/827195.html 本文适用于所有脏 ...

  5. git常用命名:自用,持续更新

    1.切换分支 git checkout -b dev origin/feature/迭代1.1 2.提交本地代码到github git init //初始化git git config --globa ...

  6. 简书 markdown 代码高亮标记

    SyntaxHighlight language language_key 1C 1c ActionScript actionscript Apache apache AppleScript a pp ...

  7. 测试linux下磁盘的读写速率

    1) 通过df -h命令查看磁盘情况 Filesystem            Size  Used Avail Use% Mounted on/dev/sda4             289G  ...

  8. [C++] 类的成员变量和成员方法

    类具有成员变量和成员方法 成员变量用来描述某个对象的具体特征,是静态的,也称为成员属性,这些属性一般设置为私有,仅供类的内部使用. 成员方法用来描述某个对象的具体行为,是动态的,也成为成员函数,这些属 ...

  9. Linux下查看目录文件数和文件大小

    一.查看当前目录下文件个数 在linux下查看目录下有多少文件可以用:ls -l  命令查看,ls -lR 递归查看所有目录, 如果文件很多,则用wc命令 和 grep 命令进行过滤. wc命令显示输 ...

  10. Python3之logging模块浅析

    Python3之logging模块浅析   目录 Python3之logging模块浅析 简单用法 日志与控制台同时输出 一个同时输出到屏幕.文件的完成例子 日志文件截取 日志重复打印问题解决 问题分 ...