360. Sort Transformed Array
一元二次方程。。。仿佛回到了初中。
主要看a的情况来分情况讨论:
=0,一次函数,根据b的正负单调递增递减就行了。
<0,凸状。。从nums[]左右两边开始往中间一边比较一边 从右往左 放;
0,凹状。。从左往右。。
public class Solution {
public int[] sortTransformedArray(int[] nums, int a, int b, int c)
{
int n = nums.length;
int[] res = new int[n];
int i = 0;
if(a == 0)
{
if(b >= 0) while(i < n) res[i] = nums[i++]*b + c;
else
while(i < n)
{
res[i] = nums[n-1-i]*b + c;
i++;
}
}
else
{
int l = 0;
int r = n-1;
int M = -b/a;
while(i < n)
{
int p = cal(nums[l],a,b,c);
int q = cal(nums[r],a,b,c);
if(a > 0)
{
if(p > q)
{
l++;
res[n-1-i] = p;
}
else
{
r--;
res[n-1-i] = q;
}
}
else
{
if(p < q)
{
l++;
res[i] = p;
}
else
{
r--;
res[i] = q;
}
}
i++;
}
}
return res;
}
public int cal(int x, int a, int b, int c)
{
return a*x*x + b*x + c;
}
}
360. Sort Transformed Array的更多相关文章
- 360. Sort Transformed Array二元一次方程返回大数序列
[抄题]: Given a sorted array of integers nums and integer values a, b and c. Apply a quadratic functio ...
- 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( ...
- 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( ...
- CF451B Sort the Array 水题
Codeforces Round #258 (Div. 2) Sort the Array B. Sort the Array time limit per test 1 second memory ...
- [CareerCup] 11.2 Sort Anagrams Array 异位词数组排序
11.2 Write a method to sort an array of strings so that all the anagrams are next to each other. 这道题 ...
随机推荐
- js获取标签的几种方式
一:id获取(全部浏览器兼容) document.getElementById(""); <body> <div id="box">&l ...
- mysql数据库容量查询
1.统计每张表的数据量SELECT *FROM ( select TABLE_NAME, concat( round( sum(DATA_LENGTH / 1024 / 1024 ), 7 ) ) a ...
- 【HDU3487】【splay分裂合并】Play with Chain
Problem Description YaoYao is fond of playing his chains. He has a chain containing n diamonds on it ...
- C#遍历所有的Textbox控件并赋值为String.Empty
foreach (Control control in this.Controls) { if (control.GetType().Name.Equals("TextBox")) ...
- ajax 文件上传,ajax
ajax 文件上传,ajax 啥也不说了,直接上代码! <input type="file" id="file" name="myfile&qu ...
- opencv数据结构总结
OpenCV里面用到了很多图像相关的数据结构,熟练掌握它们是学习图像的基础. 1.IplImage IplImage IplImage IPL 图像头 typedef struct _IplImage ...
- Ubuntu14.04 安装QQ(国际版)
1.在/etc/apt/source.list文件中添加: deb http://packages.linuxdeepin.com/deepin trusty main non-free univer ...
- Linux 环境下自动化测试工具,Redhat dogtail的安装
dogtail基于Accessibility(a11y)的GUI图形界面测试工具和自动化框架可以与linux桌面应用程序进行交互操作. dogtail是用Python语言写的.dogtail的测试脚本 ...
- psql rank row
rank() OVER (PARTITION BY f1 ORDER BY f2 DESC) ROW_NUMBER() () OVER (PARTITION BY f1 ORDER BY f2 DES ...
- windows下实现uboot的tftp下载功能
一.原理分析 带有uboot的开发板实际上充当的就是tftp客户端,而PC机扮演的角色就是tftp服务器端,而tftp下载功能实际上就是文件传输.tftp服务器可以建立在虚拟机linux下,也可以建立 ...