parent
db785168ba
commit
63e2a42d5b
|
|
@ -124,7 +124,18 @@ class Solution {
|
||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def largestSumAfterKNegations(self, A: List[int], K: int) -> int:
|
||||||
|
A = sorted(A, key=abs, reverse=True) # 将A按绝对值从大到小排列
|
||||||
|
for i in range(len(A)):
|
||||||
|
if K > 0 and A[i] < 0:
|
||||||
|
A[i] *= -1
|
||||||
|
K -= 1
|
||||||
|
if K > 0:
|
||||||
|
A[len(A) - 1] *= ((-1)**K)
|
||||||
|
return sum(A)
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue