|
25 | 25 | */
|
26 | 26 | public class _360 {
|
27 | 27 |
|
28 |
| -//credit: https://discuss.leetcode.com/topic/48424/java-o-n-incredibly-short-yet-easy-to-understand-ac-solution |
29 |
| - //in sum, only two cases: when a >= 0 or when a < 0, this simplifies logic |
30 |
| -public int[] sortTransformedArray(int[] nums, int a, int b, int c) { |
31 |
| - int n = nums.length; |
32 |
| - int[] sorted = new int[n]; |
33 |
| - int i = 0; |
34 |
| - int j = n - 1; |
35 |
| - int index = a >= 0 ? n - 1 : 0; |
36 |
| - while (i <= j) { |
37 |
| - if (a >= 0) { |
38 |
| - sorted[index--] = function(nums[i], a, b, c) >= function(nums[j], a, b, c) ? function(nums[i++], a, b, c) : function(nums[j--], a, b, c); |
39 |
| - } else { |
40 |
| - sorted[index++] = function(nums[i], a, b, c) >= function(nums[j], a, b, c) ? function(nums[j--], a, b, c) : function(nums[i++], a, b, c); |
| 28 | + public static class Solution1 { |
| 29 | + //credit: https://discuss.leetcode.com/topic/48424/java-o-n-incredibly-short-yet-easy-to-understand-ac-solution |
| 30 | + //in sum, only two cases: when a >= 0 or when a < 0, this simplifies logic |
| 31 | + public int[] sortTransformedArray(int[] nums, int a, int b, int c) { |
| 32 | + int n = nums.length; |
| 33 | + int[] sorted = new int[n]; |
| 34 | + int i = 0; |
| 35 | + int j = n - 1; |
| 36 | + int index = a >= 0 ? n - 1 : 0; |
| 37 | + while (i <= j) { |
| 38 | + if (a >= 0) { |
| 39 | + sorted[index--] = |
| 40 | + function(nums[i], a, b, c) >= function(nums[j], a, b, c) ? function( |
| 41 | + nums[i++], a, b, c) : function(nums[j--], a, b, c); |
| 42 | + } else { |
| 43 | + sorted[index++] = |
| 44 | + function(nums[i], a, b, c) >= function(nums[j], a, b, c) ? function( |
| 45 | + nums[j--], a, b, c) : function(nums[i++], a, b, c); |
| 46 | + } |
| 47 | + } |
| 48 | + return sorted; |
41 | 49 | }
|
42 |
| - } |
43 |
| - return sorted; |
44 |
| -} |
45 | 50 |
|
46 |
| - public int[] sortTransformedArray_naive(int[] nums, int a, int b, int c) { |
47 |
| - int[] result = new int[nums.length]; |
48 |
| - for (int i = 0; i < nums.length; i++) { |
49 |
| - result[i] = function(nums[i], a, b, c); |
| 51 | + private int function(int num, int a, int b, int c) { |
| 52 | + return a * (num * num) + b * num + c; |
50 | 53 | }
|
51 |
| - Arrays.sort(result); |
52 |
| - return result; |
53 | 54 | }
|
54 | 55 |
|
55 |
| - private int function(int num, int a, int b, int c) { |
56 |
| - return a * (num * num) + b * num + c; |
| 56 | + public static class Solution2 { |
| 57 | + public int[] sortTransformedArray(int[] nums, int a, int b, int c) { |
| 58 | + int[] result = new int[nums.length]; |
| 59 | + for (int i = 0; i < nums.length; i++) { |
| 60 | + result[i] = function(nums[i], a, b, c); |
| 61 | + } |
| 62 | + Arrays.sort(result); |
| 63 | + return result; |
| 64 | + } |
| 65 | + |
| 66 | + private int function(int num, int a, int b, int c) { |
| 67 | + return a * (num * num) + b * num + c; |
| 68 | + } |
57 | 69 | }
|
58 | 70 |
|
59 | 71 | }
|
0 commit comments