[抄题]:

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 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]:

[思维问题]:

不知道和指针对撞有啥关系:谁的平方比较大(绝对值大)数组就先加谁

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 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二元一次方程返回大数序列的更多相关文章

  1. LeetCode 360. Sort Transformed Array

    原题链接在这里:https://leetcode.com/problems/sort-transformed-array/description/ 题目: Given a sorted array o ...

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

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

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

  6. 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. [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 ...

  9. CF451B Sort the Array 水题

    Codeforces Round #258 (Div. 2) Sort the Array B. Sort the Array time limit per test 1 second memory ...

随机推荐

  1. sails.js mvc framework learning

    目的:加快开发速度,总结使用方法: menu list: custom controller custom 模块使用 custom model custom middleware custom ser ...

  2. c# android 全局捕获未处理异常

    [Application] public class MyApp : Application { public MyApp(IntPtr javaReference, JniHandleOwnersh ...

  3. docker基本命令使用

    学会使用docker命令帮助 docker help 子命令 查看docker镜像 docker images 搜索镜像 docker search 镜像名 下载镜像 docker pull 镜像名 ...

  4. Python字符串列表元祖字典的公共方法

    运算符 运算符 Python 表达式 结果 描述 支持的数据类型 + [1, 2] + [3, 4] [1, 2, 3, 4] 合并 字符串.列表.元组 * 'Hi!' * 4 ['Hi!', 'Hi ...

  5. phpize是什么

    安装php(fastcgi模式)的时候,常常有这样一句命令:/usr/local/webserver/php/bin/phpize一.phpize是干嘛的?phpize是什么东西呢?php官方的说明: ...

  6. IntelliJ IDEA中Terminal路径的问题(win7环境)

    在安装java jdk,配置系统变量后,再安装idea,有时候会出现使用idea中Termimal进行编译运行java文件出现,javac/java不是内部命令,或者“错误: 找不到或无法加载主类”的 ...

  7. 涨姿势:Java 分业务、分级别实现自定义日志打印

    自定义日志级别 通常的日志框架都有以下几个级别,从低到高TRACE,DEBUG,INFO,WARN,ERROR,FATAL. 默认情况,假如我们定义日志打印级别INFO,它会把大于等于INFO级别的日 ...

  8. Struts2 中常用的代码

    BaseAction public class BaseAction extends ActionSupport { protected String target; public Map getRe ...

  9. kubernetes学习笔记之十:RBAC

    第一章.RBAC介绍 在Kubernetes中,授权有ABAC(基于属性的访问控制).RBAC(基于角色的访问控制).Webhook.Node.AlwaysDeny(一直拒绝)和AlwaysAllow ...

  10. mybatis 注解形式设置批量新增、批量更新数据

    1. 批量更新: @Update({"<script>" + "<foreach collection=\"smsConfigTemplate ...