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 ...
随机推荐
- 算法笔记 3.2 codeup1934 找X
#include <stdio.h> ; int a[maxn]; int main(void){ int n; while(scanf("%d", &n)!= ...
- RDO快速部署OpenStack
RDO快速部署OpenStack 1.RDO是什么 RDO是红帽Red Hat Enterprise Linux OpenStack Platform的社区版,类似RHEL和Fedora,RHEV和o ...
- 洛谷P1605走迷宫
传送 这是一道dfs,但是...但是....但是它竟然被放在bfs练习题辣!!!! 打了半天bfs,发现路径不会标记了,于是发现好像有什么不对的,似乎dfs要简单一点,于是半路跑去打dfs,结果打了半 ...
- Pod配置PersistentVolumeClaim详解
1,创建PersistentVolume kind: PersistentVolume apiVersion: v1 metadata: name: task-pv-volume labels: ty ...
- 解决Ubuntu中文显示为乱码
1. 安装所需软件 sudo apt-get install zh-autoconvert sudo apt-get install zhcon 2. 配置系统 $ vi /var/lib/local ...
- django模型系统(三)
1,自定义主键字段的创建 AutoFiled(pirmary_key=True) # 一般不会自定义 2,order_by asc desc 表关系 OneToOne student = mod ...
- [UE4]Image
一.Image.Appearance.Brush.Tiling:平铺方式 1.No Tile:不平铺,拉伸会变形 2.Horizontal:横向平铺.纵向拉伸会变形 3.Vertical:纵向平铺.横 ...
- POJ1003 – Hangover (基础)
Hangover Description How far can you make a stack of cards overhang a table? If you have one card, ...
- Git分支merge和rebase的区别
Git merge是用来合并两个分支的. git merge b # 将b分支合并到当前分支 同样 git rebase b,也是把 b分支合并到当前分支 原理 如下: 假设你现在基于远程分支&quo ...
- [持续交付实践] 开篇:持续集成&持续交付综述
前言 随着微服务架构与容器虚拟化技术的发展,持续集成与持续交付的概念又重新回到了大家的视野,越来越多的公司开始使用持续集成的系统来解决频繁发布带来的质量问题:使用持续交付的工具来实现代码在不同环境上的 ...