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 ...
随机推荐
- 使用parted对大于2T的磁盘进行分区
使用parted对磁盘进行分区 版本信息 版本 修改日期 修改人 修改内容 备注 V0.1 2018/09/06 初始化版本 讨论稿 ...
- (翻译).NET应用架构
.NET应用架构 Kalyan Bandarupalli著,hystar翻译 这个系列文章将帮助.NET开发人员与架构师使用最新的.NET技术设计高效的.NET应用.关于应用架构这方面虽然已有很多文章 ...
- @RequestParam接收解析不到 POST 提交的 数据
1.使用postman或者其他发送请求模拟器进行模拟访问,需要指定Headers为Content-Type:application/x-www-form-urlencoded;指定body类型为x-w ...
- ssh 制作免密登录(免密登录多台服务器)
场景:服务器B (192.168.1.101)免密登录A(192.168.1.100)服务器 1.登录服务器B ①生产公钥 ssh-keygen -t rsa Enter file in which ...
- <ROS> 机器人描述--URDF和XACRO
文章转自 https://blog.csdn.net/sunbibei/article/details/52297524 特此鸣谢原创作者的辛勤付出 1 URDF 文件 1.1 link和joint ...
- ionic3 热更新发布步骤记录
1.安装基本框架npm install -g ionic@latest npm install -g cordova ionic 验证版本号 ionic –version cordova -versi ...
- 解决Kubelet Pod启动CreatePodSandbox或RunPodSandbox异常方法
新装Kubernetes,创建一个新Pod,启动Pod遇到CreatePodSandbox或RunPodSandbox异常.查看日志 # journalctl --since :: -u kubele ...
- Excel函数匹配查找
需求 例如:北京沃尔玛有限公司,由已知的沃尔玛缩写,将两者进行匹配. 函数 lookup() 第一个参数“Lookup_value”:是要查找的值:第二个参数“lookup_vector”:是要查找的 ...
- Scrapy实战篇(一)之爬取链家网成交房源数据(上)
今天,我们就以链家网南京地区为例,来学习爬取链家网的成交房源数据. 这里推荐使用火狐浏览器,并且安装firebug和firepath两款插件,你会发现,这两款插件会给我们后续的数据提取带来很大的方便. ...
- python:id与小数据池与编码
一.id与小数据池 id:查的是内存地址 a = 100 b = 100 print(a == b)#比较的数值 print(a is b)#比较的是id print(id(a),id(b))#id相 ...