您的位置:首页 > 其它

Sort Transformed Array

2016-06-27 12:49 387 查看
public class Solution {
public int[] sortTransformedArray(int[] nums, int a, int b, int c) {
if (nums.length < 2) {
return nums;
}
int[] result = new int[nums.length];
int start = 0, end = nums.length - 1;
int index = a > 0 ? nums.length - 1 : 0;
while (start <= end) {
if (a > 0) {
result[index--] = getF(nums[start], a, b, c) > getF(nums[end], a, b, c) ? getF(nums[start++], a, b, c) : getF(nums[end--], a, b, c);
} else {
result[index++] = getF(nums[start], a, b, c) < getF(nums[end], a, b, c) ? getF(nums[start++], a, b, c) : getF(nums[end--], a, b, c);
}
}
return result;
}

private int getF(int x, int a, int b, int c) {
return a * x * x + b * x + c;
}
}


1. This is a parabola function. So two ends always larger than mid.

2. remember return value.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: