Skip to content

Commit c156a08

Browse files
authored
Update sort-transformed-array.py
1 parent ab15f0c commit c156a08

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

Python/sort-transformed-array.py

+9-17
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,13 @@ def sortTransformedArray(self, nums, a, b, c):
1717
return result
1818

1919
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-
else:
29-
while left <= right:
30-
if f(nums[left], a, b, c) < f(nums[right], a, b, c):
31-
result.append(f(nums[left], a, b, c))
32-
left += 1
33-
else:
34-
result.append(f(nums[right], a, b, c))
35-
right -= 1
20+
d = -1 if a > 0 else 1
21+
while left <= right:
22+
if d * f(nums[left], a, b, c) < d * 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
3628

37-
return result if a <= 0 else result[::-1]
29+
return result[::d]

0 commit comments

Comments
 (0)