原题链接在这里:https://leetcode.com/problems/sort-transformed-array/description/

题目:

Given a sorted array of integers nums and integer values ab and c. Apply a quadratic 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]

题解:

如果a > 0, 左右两边的点就比中间的点大. two pointers夹比, 从大往小赋值.

a < 0, 左右两边的点比中间的点小. two pointers夹比, 从小往大赋值.

Time Complexity: O(n). n = nums.length.

Space: O(1). regardless res.

AC Java:

 class Solution {
public int[] sortTransformedArray(int[] nums, int a, int b, int c) {
if(nums == null || nums.length == 0){
return nums;
} int [] res = new int[nums.length];
int l = 0;
int r = nums.length-1;
int index = a >=0 ? nums.length-1 : 0; while(l <= r){
int lNum = quad(nums[l], a, b, c);
int rNum = quad(nums[r], a, b, c);
if(a>=0){
if(lNum > rNum){
res[index--] = lNum;
l++;
}else{
res[index--] = rNum;
r--;
}
}else{
if(lNum > rNum){
res[index++] = rNum;
r--;
}else{
res[index++] = lNum;
l++;
}
}
} return res;
} private int quad(int x, int a, int b, int c){
return a*x*x + b*x + c;
}
}

类似Squares of a Sorted Array.

LeetCode 360. Sort Transformed Array的更多相关文章

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

  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] Sort Transformed Array 变换数组排序

    Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...

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

  8. 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. Leetcode 912. Sort an Array

    class Solution: def sortArray(self, nums: List[int]) -> List[int]: return sorted(nums)

随机推荐

  1. 学习 nginx (持续更新)

    什么是代理与反向代理,有什么应用场景? 平常经常听别人说代理与反向代理,那么这二者到底有什么区别呢? 代理 场景:我需要访问一个服务器C,但是由于某些原因我无法访问到它,(典型的就是你FQ,然后fai ...

  2. LightOJ - 1151概率dp+高斯消元

    概率dp+高斯消元 https://vjudge.net/problem/LightOJ-1151 题意:刚开始在1,要走到100,每次走的距离1-6,超过100重来,有一些点可能有传送点,可以传送到 ...

  3. yii2手动添加插件PHPExcel

    1.下载地址:https://github.com/PHPOffice/PHPExcel 2.解压并修改文件名为phpexcel 之后在yii项目的vendor目录下创建一个文件夹命名为phpoffi ...

  4. ansible入门七(实战)

    Ansible实战:部署分布式日志系统   本节内容: 背景 分布式日志系统架构图 创建和使用roles JDK 7 role JDK 8 role Zookeeper role Kafka role ...

  5. MYSQL freedata 外联接

    主要是解决,不同生产系统里面,有不同的数据库. SQL 又不能夸系统查询表. 只能在一个系统里,可以跨不同的数据库查表. 所以会用映射 .FREEDATA 这种方式,这样A 系统 里的表更新之后,就可 ...

  6. A网页高度随B内容而自然变化兼容各种浏览器

    今天是星期一,昨天又没有睡好,眼睛干干的,还有点痛疼,于是不想干啥.但是心里又不觉得过意不去,浪费时间呀,考虑再三把自己前面实现的一个东西发了上来.算是今天的功劳,没有过多的文字描述,纯粹是贴图片和代 ...

  7. CF 914

    照例看A 然后A了 看B 似乎博弈一下就可以了 然后看C 似乎是DP 然后看了room woc似乎有黑红名 赶紧hack 然后没有人有问题 思考为什么 突然看到房间有15hack... 好吧我做D 然 ...

  8. hdu3718

    题解: 见图 按照每一个位置上有相同加一 然后km 代码: #include<cstdio> #include<cmath> #include<cstring> # ...

  9. ZOJ 1609 Equivalence(状压+dfs减枝)

    ZOJ Problem Set - 1609 Equivalence Time Limit: 5 Seconds      Memory Limit: 32768 KB When learning m ...

  10. crypt函数加密验证

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...