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. 这道题 ...
随机推荐
- javascript——操作符(~、&、|、^、<<、>>)
直接上代码吧! <script type="text/javascript"> //javascript操作符 //1.按位非~ var num1=25;// var ...
- Oracle IN 传递字符串参数查询失效
在写存储过程中有如下代码: FOR a IN ( SELECT a.svo_no,a.AUDIT_NO,a.order_id FROM TT_PI_MODEL_REL a ) LOOP SELECT ...
- photoshop中rgb与索引模式的区别
RGB: 是色域最宽广的颜色模式它是一种光色模式不难理解 你的显示器 就是根据这个模式来显示图像的同样 我们在自然界中 看到的所有五颜六色的东西都是吸收了不同颜色的光 而得到的所以这一颜色模式 与我 ...
- OC之JSON数据解析
JSON介绍: 作为一种轻量级的数据交换格式,正在逐步取代XML,成为网络数据的通用格式 基于JavaScript的一个子集 易读性略差,编码手写难度大,数据量小 JSON格式取代了XML给网络传输带 ...
- Sphinx 排序模式 SetSortMode
可使用如下模式对搜索结果排序: SPH_SORT_RELEVANCE 模式, 按相关度降序排列(最好的匹配排在最前面) SPH_SORT_ATTR_DESC 模式, 按属性降序排列 (属性值越大的越是 ...
- delphi定义自己的消息
定义一个消息需要两个步骤: 1.声明一个消息标识符 2.声明一个消息记录类型 一个消息标识符是一个整数大小的常数.Windows自用低于1024的消息,所以当你声明你自己的消息,你应该开始高于这一数字 ...
- Ruby自学笔记(五)— 条件判断
条件判断,在编程语言中都存在,而Ruby中的条件判断和Java中类似,当然还是存在些许差别 Ruby中条件判断的条件: 1) 可以使用 ==,<,>等比较运算来作为条件,比较运算可以返回t ...
- Unity NGUI 3.0.4版本 制作网络版斗地主
Unity NGUI 3.0.4版本 @by 灰太龙 开发环境 Win7旗舰版 Unity 4.2.1f4 本文就写个开门篇,告诉大家怎么用NGUI,第一步导入NGUI 3.0.4版本! 1.启动U ...
- Java Integer类分析
public static final int MIN_VALUE = 0x80000000; -2^31 public static final int MAX_VALUE = 0x7ff ...
- [置顶] 技术人血泪史:七种IT失误让你直接走人
IT人士的真实故事:搞出大麻烦,旋即遭解雇 如今想找一份理想的IT工作并不容易,但丢掉一份工作却非常简单. 导致自己被炒鱿鱼的原因很多,无论是没能尽到保护雇主数字资产的义务.或者是滥用手中的权限以达到 ...