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常用标准库函数

    math库: >>> import math >>> dir(math) ['__doc__', '__loader__', '__name__', '__pack ...

  2. Java基础(八 前面补充)

    1.笔记 1.局部内部类 如果一个类是定义在一个方法内部的,那么这就是一个局部内部类. “局部”:只有当前所属的方法才能使用它,出了这个方法外面就不能用了. 定义格式: 修饰符 class 外部类名称 ...

  3. 03-Flutter移动电商实战-底部导航栏制作

    1.cupertino_IOS风格介绍 在Flutter里是有两种内置风格的: material风格: Material Design 是由 Google 推出的全新设计语言,这种设计语言是为手机.平 ...

  4. 洛谷 P2512 [HAOI2008]糖果传递 题解

    每日一题 day47 打卡 Analysis 首先,最终每个小朋友的糖果数量可以计算出来,等于糖果总数除以n,用ave表示. 假设标号为i的小朋友开始有Ai颗糖果,Xi表示第i个小朋友给了第i-1个小 ...

  5. MySQL 分库分表 dble简单使用

    一.运行环境 Host Name IP DB Mod data0 172.16.100.170 mysql   data1 172.16.100.171 mysql   data2 172.16.10 ...

  6. hibernate的API

    程序源码: import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transa ...

  7. URL的作用是什么?它由几部分组成?

    URL是统一资源定位符,对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网上标准资源的地址.互联网上的每个文件都有一个唯一的URL,它包含的信息指出文件的位置以及浏览器应该怎么处理它 ...

  8. 无人机一体化3DGIS服务平台

    随着无人机技术的发展,无人机携带多种设备为GIS应用提供多元化海量基础数据.无人机航测更是以快速.灵活.高效的数据获取方式,迅速扩大了现有的GIS市场,同时GIS行业的广泛应用也推动了无人机技术的发展 ...

  9. CTF PHP反序列化

    目录 php反序列化 一.序列化 二.魔术方法 1.构造函数和析构函数 2.__sleep()和__wakeup() 3.__toString() 4.__set(), __get(), __isse ...

  10. UML的使用

    软件工程项目这周要交一个设计文档,其中涉及UML图的画法,根据上课给的ppt做一个记录. 有关于UML的介绍在这里不再赘述,直接开整! UML的基本模型 当然必要的介绍必不可少,这里先介绍UML的基本 ...