原题链接在这里: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. 图 Graph-图的表示及其遍历

    2018-03-05 16:19:46 图是计算机科学中的一个非常重要的概念,图是一种多对多的关系.从某种角度上来说树和链表都是图的一种特例. 一.图的抽象数据类型 二.表示图的方法 图是由结点和边构 ...

  2. 分布式MySql

    # 分布式MySql 部署方案---1. 解决方案2. 系统环境3. mysql 主从备份4. MyCat 中间件搭建5. haproxy 负载代理6. keepalived 解决单点故障7. myc ...

  3. 安装Linux环境

    虚拟机:虚拟机(Virtual Machine),在计算机科学中的体系结构里,是指一种特殊的软件,他可以在计算机平台和终端用户之间建立一种环境,而终端用户则是基于这个软件所建立的环境来操作软件.在计算 ...

  4. Shell test 命令,Shell 输入/输出重定向

    一.Shell test 命令 Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值.字符和文件三个方面的测试. 数值测试 参数 说明 -eq 等于则为真 -ne 不等于则为真 -g ...

  5. 无密码登陆的ssh和ssh-agent

    原文地址:http://lxshopping.blog.51cto.com/4542643/1179864/ 一,不需要输密码的ssh 原理:首先服务器端把公钥传给Client端,Client端在验证 ...

  6. (转)让IE6/IE7/IE8浏览器支持CSS3属性

    原文链接 http://blog.csdn.net/h5_queenstyle12/article/details/50437442 一.下载 搜索下载:ie-css3.htc,它是让IE浏览器支持C ...

  7. C#运行批处理【转】

    /// <summary> /// C#运行bat文件 /// </summary> /// <param name="batPath">< ...

  8. 第9课:备份mysql数据库、重写父类、unittest框架、多线程

    1. 写代码备份mysql数据库: 1)Linux下,备份mysql数据库,在shell下执行命令:mysqldump -uroot -p123456 -A >db_bak.sql即可 impo ...

  9. this指针逃逸问题

    this指针逃逸是指在构造函数返回之前,其他线程已经就持有了该对象的应用,产生的结果自然和预期可能会产生差异.常见的this指针逃逸,在并发编程实战一书中,作者指出:在构造函数中注册事件监听,在构造函 ...

  10. xss 防御

    系列 防御原则 第一.在输入方面对所有用户提交内容进行可靠的输入验证,提交内容包括URL.查询关键字.http头.post数据等 第二.在输出方面,在用户输内容中使用 <XMP>标签 还是 ...