标签:
1 public class Solution { 2 public int[] sortTransformedArray(int[] nums, int a, int b, int c) { 3 if (nums.length < 2) { 4 return nums; 5 } 6 int[] result = new int[nums.length]; 7 int start = 0, end = nums.length - 1; 8 int index = a > 0 ? nums.length - 1 : 0; 9 while (start <= end) { 10 if (a > 0) { 11 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); 12 } else { 13 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); 14 } 15 } 16 return result; 17 } 18 19 private int getF(int x, int a, int b, int c) { 20 return a * x * x + b * x + c; 21 } 22 }
1. This is a parabola function. So two ends always larger than mid.
2. remember return value.
标签:
原文地址:http://www.cnblogs.com/shuashuashua/p/5619845.html