-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC360.cpp
47 lines (40 loc) · 1.19 KB
/
LC360.cpp
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
46
47
//
// Created by hf46p on 2016/6/21.
//
#include "IncludeHelper.h";
class Solution {
private:
int foo(int x, int a, int b, int c) {
return a*x*x + b*x + c;
}
public:
vector<int> sortTransformedArray(vector<int>& nums, int a, int b, int c) {
vector<int> res;
if (nums.empty()) return res;
double y = -(double)b/(a*2);
int i = 0, j = nums.size()- 1;
if (a >= 0) {
while (i<=j) {
if (foo(nums[i], a, b, c) > foo(nums[j], a, b, c)) {
res.push_back(foo(nums[i++], a, b, c));
} else {
res.push_back(foo(nums[j--], a, b, c));
}
}
for (int i=0;i<res.size()/2;++i) {
int t = res[i];
res[i] = res[res.size()-i-1];
res[res.size()-i-1] = t;
}
} else if (a < 0) {
while (i<=j) {
if (foo(nums[i], a, b, c) < foo(nums[j], a, b, c)) {
res.push_back(foo(nums[i++], a, b, c));
} else {
res.push_back(foo(nums[j--], a, b, c));
}
}
}
return res;
}
};