forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
34 lines (33 loc) · 911 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution {
public int[] sortTransformedArray(int[] nums, int a, int b, int c) {
int n = nums.length;
int i = 0, j = n - 1, k = a < 0 ? 0 : n - 1;
int[] res = new int[n];
while (i <= j) {
int v1 = f(a, b, c, nums[i]), v2 = f(a, b, c, nums[j]);
if (a < 0) {
if (v1 <= v2) {
res[k] = v1;
++i;
} else {
res[k] = v2;
--j;
}
++k;
} else {
if (v1 >= v2) {
res[k] = v1;
++i;
} else {
res[k] = v2;
--j;
}
--k;
}
}
return res;
}
private int f(int a, int b, int c, int x) {
return a * x * x + b * x + c;
}
}