Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4e4f547

Browse files
refactor 360
1 parent 99613a5 commit 4e4f547

File tree

1 file changed

+36
-24
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+36
-24
lines changed

src/main/java/com/fishercoder/solutions/_360.java

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,47 @@
2525
*/
2626
public class _360 {
2727

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;
4149
}
42-
}
43-
return sorted;
44-
}
4550

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;
5053
}
51-
Arrays.sort(result);
52-
return result;
5354
}
5455

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+
}
5769
}
5870

5971
}

0 commit comments

Comments
 (0)