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. 这道题 ...
随机推荐
- eclipse build很慢的时候,有可能是js文件编译验证慢的问题
第一步: 去除eclipse的JS验证: 将windows->preference->Java Script->Validator->Errors/Warnings-> ...
- java异步上传图片
第一步:引入需要的js <script src="/res/common/js/jquery.js" type="text/javascript"> ...
- express不是内部命令解决办法
安装nodejs后,安装express,cmd下命令:npm install express -g;之后敲入express -V会提示不是内部命令,是因为, 4.0版本中将命令工具分家出来了,所以我们 ...
- 大规模字符串检索-压缩trie树
本文使用压缩trie树实现字符串检索的功能.首先将字符串通过编码转化为二进制串,随后将二进制串插入到trie树中,在插入过程中同时实现压缩的功能. 字符编码采用Huffman,但最终测试发现不采用Hu ...
- newInstance()和new的区别
在初始化一个类,生成一个实例的时候:newInstance() 和 new 有什么区别?用newInstance与用new是有区别的,区别在于创建对象的方式不一样,前者是使用类加载机制,那么为什么会有 ...
- java获取数据库数据表的元数据
Connction conn; DatabaseMetaData dmd=conn.getMetaData();//获取数据库元数据 PreparedStatment ps; ps.getParame ...
- mysqldump: command not found问题解决
首先得知道mysql命令或mysqldump命令的完整路径,可以使用find命令查找除非你知道mysql安装路径可以略过这一步. find / -name mysql -print 例如我的mysql ...
- js文字向上滚动代码
js文字向上滚动代码 <style>.pczt_pingfen_jhxs_news1{ width:397px; background:#edfafd; padding-top:2px; ...
- Cobar介绍及配置
from:http://code.alibabatech.com/wiki/display/cobar/Home Skip to end of metadata Page restrictions ...
- 抽象数据类型Triplet的C语言实现
#include <stdio.h> #include <stdlib.h> #define ERROR 0 #define OK 1 typedef int Status; ...