360. Sort Transformed Array二元一次方程返回大数序列
[抄题]:
Given a sorted array of integers nums and integer values a, b 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 1:
Input: nums = [-4,-2,2,4], a = 1, b = 3, c = 5
Output: [3,9,15,33]
Example 2:
Input: nums = [-4,-2,2,4], a = -1, b = 3, c = 5
Output: [-23,-5,1,7]
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道和指针对撞有啥关系:谁的平方比较大(绝对值大)数组就先加谁
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- int startIndex = (a >= 0) ? nums.length - 1 : 0; 变量声明必须写在最前面,不能写在里面
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
不知道和指针对撞有啥关系:谁的平方比较大(绝对值大)数组就先加谁
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public int[] sortTransformedArray(int[] nums, int a, int b, int c) {
//initialization: int[] nums, i & j
int[] sorted = new int[nums.length];
Arrays.sort(nums);
//corner case
if (nums == null || nums.length == 0) return sorted;
int i = 0; int j = nums.length - 1;
//initialization: startIndex, depend on a
//must be in one-line
int startIndex = (a >= 0) ? nums.length - 1 : 0;
//while i <= j, add to result according to a
while (i <= j) {
if (a >= 0) {
sorted[startIndex--] = quad(a, b, c, nums[i]) > quad(a, b, c, nums[j]) ? quad(a, b, c, nums[i++]) : quad(a, b, c, nums[j--]);
}else {
sorted[startIndex++] = quad(a, b, c, nums[i]) > quad(a, b, c, nums[j]) ? quad(a, b, c, nums[j--]) : quad(a, b, c, nums[i++]);
}
}
//return
return sorted;
}
public int quad(int a, int b, int c, int x) {
return a * x * x + b * x + c;
}
}
360. Sort Transformed Array二元一次方程返回大数序列的更多相关文章
- LeetCode 360. Sort Transformed Array
原题链接在这里:https://leetcode.com/problems/sort-transformed-array/description/ 题目: Given a sorted array o ...
- [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( ...
- 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] 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
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( ...
- [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 ...
- CF451B Sort the Array 水题
Codeforces Round #258 (Div. 2) Sort the Array B. Sort the Array time limit per test 1 second memory ...
随机推荐
- CentOS7下安装Python3及Pip3并保留Python2
1. 安装依赖环境 # yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline- ...
- 巧妇难为无米之炊( Model数据)
一.相隔万里的客户端服务器数据交互 请求头发过去的轻量级文本数据,后台根据这些信息处理 response返回的如果时html的话,那么是全局刷新 在ajax中data回调获得了数据,然后操作dom进 ...
- C-Language Functions
转自:https://www.postgresql.org/docs/9.6/xfunc-c.html 可以作为学习基于c编写pg extension 的资料 36.9. C-Language Fun ...
- Java面向对象 第3节 类的封装和继承
一.封装 封装的概念:将类的某些信息隐藏在类内部,不允许外部程序直接访问,而是通过该类提供的方法来实现对隐藏信息的访问和操作. 封装的2个大致原则:1)把尽可能多的东西隐藏起来,对外提供便捷的接口 ...
- mysql 启动失败,数据恢复
mysql 启动失败,数据恢复 2017年02月13日 16:46:36 阅读数:621 Forcing InnoDB Recovery提供了6个等级的修复模式,需要注意的是值大于3的时候,会对数据文 ...
- myibatis的坑--text类型的字段查询缺失
问题:某个字段的类型为text(或者mediumtext,longtext)的时候,用selectByQuery语句查询出来的结果不包含该字段内容. myibatis 用mybatis-generat ...
- 初识TypeScript
环境配置 1,全局安装node和npm 2,安装TypeScript包 npm install typescript -g tsc --version 编写第一个ts程序 1,初始化项目 新建文件夹d ...
- 当一个HTML元素需要添加mouseon、mouseout与click事件,或者mouserenter、mouseleave和click事件时,click事件无法触发
当一个HTML元素需要添加mouseon.mouseout与click事件,或者mouserenter.mouseleave和click事件时,click事件无法触发 针对上述问题,我遇到的有两种情况 ...
- ARM920T的Cache
转载自:http://www.eefocus.com/mcu-dsp/242034 ARM920T有16K的数据Cache和16K的指令Cache,这两个Cache是基本相同的,数据Cache多了一些 ...
- 三台linux集群hadoop,在此上面运行hive
---恢复内容开始--- 一,准备 先有三台linux,对hadoop集群的搭建. eddy01:开启一个hdfs的老大namenode,yarn的老大ResourceManager其中进程包括(No ...