-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathsortTransformedArray.java
45 lines (43 loc) · 1.12 KB
/
sortTransformedArray.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
35
36
37
38
39
40
41
42
43
44
45
class Solution {
public int[] sortTransformedArray(int[] nums, int a, int b, int c) {
int [] res = new int[nums.length];
int start =0, end = nums.length-1;
int i = a>=0 ? nums.length-1:0;
while(start <= end)
{
int startNum = getNum(nums[start], a, b, c);
int endNum = getNum(nums[end], a, b, c);
if(a >= 0)
{
if(startNum >= endNum)
{
res[i--] = startNum;
start++;
}
else
{
res[i--] = endNum;
end--;
}
}
else
{
if(startNum <= endNum)
{
res[i++] = startNum;
start++;
}
else
{
res[i++] = endNum;
end--;
}
}
}
return res;
}
public int getNum(int x, int a, int b, int c)
{
return a * x * x + b * x + c;
}
}