Skip to content

Commit ab15f0c

Browse files
authored
Update sort-transformed-array.cpp
1 parent 202b5bb commit ab15f0c

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

C++/sort-transformed-array.cpp

+8-15
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,16 @@ class Solution {
1414
}
1515

1616
int left = 0, right = nums.size() - 1;
17-
if (a > 0) {
18-
while (left <= right) {
19-
if (f(nums[left], a, b, c) > f(nums[right], a, b, c)) {
20-
result.emplace_back(f(nums[left++], a, b, c));
21-
} else {
22-
result.emplace_back(f(nums[right--], a, b, c));
23-
}
17+
int d = a > 0 ? -1 : 1;
18+
while (left <= right) {
19+
if (d * f(nums[left], a, b, c) < d * f(nums[right], a, b, c)) {
20+
result.emplace_back(f(nums[left++], a, b, c));
21+
} else {
22+
result.emplace_back(f(nums[right--], a, b, c));
2423
}
24+
}
25+
if (d == -1) {
2526
reverse(result.begin(), result.end());
26-
} else {
27-
while (left <= right) {
28-
if (f(nums[left], a, b, c) < f(nums[right], a, b, c)) {
29-
result.emplace_back(f(nums[left++], a, b, c));
30-
} else {
31-
result.emplace_back(f(nums[right--], a, b, c));
32-
}
33-
}
3427
}
3528

3629
return result;

0 commit comments

Comments
 (0)