Given a sorted array of integers nums and integer values a, b and c. Apply a 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]

参考:https://discuss.leetcode.com/topic/48424/java-o-n-incredibly-short-yet-easy-to-understand-ac-solution

the problem seems to have many cases a>0, a=0,a<0, (when a=0, b>0, b<0). However, they can be combined into just 2 cases: a>0 or a<0

1.a>0, two ends in original array are bigger than center if you learned middle school math before.

2.a<0, center is bigger than two ends.

so use two pointers i, j and do a merge-sort like process. depending on sign of a, you may want to start from the beginning or end of the transformed array. For a==0 case, it does not matter what b's sign is.
The function is monotonically increasing or decreasing. you can start with either beginning or end.

 public class Solution {
public int[] sortTransformedArray(int[] nums, int a, int b, int c) {
int[] res = new int[nums.length];
int l=0, r=nums.length-1;
int index = a<0? 0 : nums.length-1;
while (l <= r) {
if (a < 0) {
res[index++] = calc(nums[l], a, b, c)<calc(nums[r], a, b, c)? calc(nums[l++], a, b, c) : calc(nums[r--], a, b, c);
}
else {
res[index--] = calc(nums[l], a, b, c)>calc(nums[r], a, b, c)? calc(nums[l++], a, b, c) : calc(nums[r--], a, b, c);
}
}
return res;
} public int calc(int n, int a, int b, int c) {
return a*n*n+b*n+c;
}
}

Leetcode: Sort Transformed Array的更多相关文章

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

  2. LeetCode 360. Sort Transformed Array

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

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

  4. 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( ...

  5. 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. 360. Sort Transformed Array二元一次方程返回大数序列

    [抄题]: Given a sorted array of integers nums and integer values a, b and c. Apply a quadratic functio ...

  7. 360. Sort Transformed Array

    一元二次方程...仿佛回到了初中. 主要看a的情况来分情况讨论: =0,一次函数,根据b的正负单调递增递减就行了. <0,凸状..从nums[]左右两边开始往中间一边比较一边 从右往左 放: 0 ...

  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. 【LeetCode】912. Sort an Array 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数排序 桶排序 红黑树排序 归并排序 快速排序 ...

随机推荐

  1. 关于在程序中 文件新生成 在用os.system()程序对新生成的文件处理 举个栗子 如下:

    print 'save to ',savedir+os.sep+d["FILE_NAME"]                ff = open(savedir+os.sep+d[& ...

  2. BZOJ4712 : 洪水

    首先不难列出DP方程: $dp[x]=\min(w[x],h[x])$ $h[x]=\sum dp[son]$ 当$w[x]$增加时,显然$dp[x]$不会减少,那么我们求出$dp[x]$的增量$de ...

  3. 【Linux】vi 命令

    基本上 vi/vim 共分为三种模式,分别是一般模式.编辑模式与指令列命令模式. 这三种模式的作用分别是:     一般模式:以 vi 打开一个档案就直接进入一般模式了(这是默认的模式).在这个模式中 ...

  4. UML图示说明

    UML图示说明 继承:实线空三角形箭头指向父类 接口:虚线空三角形箭头指向接口 关联:实线无箭头方指向拥有者 聚合:实线空心菱形箭头指向整体,部分可以单独存在 组合:实线菱形指向整体,部分不能单独存在 ...

  5. Linux查看进程PID信息

    ps -ef|grep 进程名 可检索到具体进程PID以及启动命令行信息 ls -l /proc/进程ID Linux在启动程序时会在 /proc/PID 目录下以PID为名称创建一个文件存储相关进程 ...

  6. jqgrid 中的事件

    1. var obj = $("#tablename").jqGrid("getRowData"); 取得所有的行     alert(obj.length); ...

  7. css 温故而知新 定位(position)与权限(z-index)

    1.进行定位(position)的元素的权限(z-index)永远比没有定位的高. 2.如果两个元素都定位了,无论是相对定位还是绝对定位.他们的权限都是等权的. 3.两个相同定位的元素,除了z-ind ...

  8. IE8浏览器不能识别CSS伪类的解决办法。

    1. 方法一:开头加上这两句 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...

  9. android——判断网络状态

    平常我们玩qq的时候我们没联网 ,qq上面回显示"网络连接不给力,请检查网络连接"那么它是 通过什么方式判断网络是否连接的呢? 下面将写个案例展示如何检查网络连接状态的 由于我们这 ...

  10. linux建立文件夹软连接

    linux建立文件夹软连接,并强制覆盖 ln -sfn /home/var/log/httpd/logs logs 这将在当前目录下建立logs软连接,指向/home/var/log/httpd/lo ...