We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ab15f0c commit c156a08Copy full SHA for c156a08
Python/sort-transformed-array.py
@@ -17,21 +17,13 @@ def sortTransformedArray(self, nums, a, b, c):
17
return result
18
19
left, right = 0, len(nums) - 1
20
- if a > 0:
21
- while left <= right:
22
- if f(nums[left], a, b, c) > f(nums[right], a, b, c):
23
- result.append(f(nums[left], a, b, c))
24
- left += 1
25
- else:
26
- result.append(f(nums[right], a, b, c))
27
- right -= 1
28
29
30
- if f(nums[left], a, b, c) < f(nums[right], a, b, c):
31
32
33
34
35
+ d = -1 if a > 0 else 1
+ while left <= right:
+ if d * f(nums[left], a, b, c) < d * f(nums[right], a, b, c):
+ result.append(f(nums[left], a, b, c))
+ left += 1
+ else:
+ result.append(f(nums[right], a, b, c))
+ right -= 1
36
37
- return result if a <= 0 else result[::-1]
+ return result[::d]
0 commit comments