Skip to content

Commit 72b2521

Browse files
committed
1005.K次取反.md Javascript
1 parent 5043002 commit 72b2521

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

problems/1005.K次取反后最大化的数组和.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,29 @@ class Solution:
140140
Go:
141141

142142

143+
Javascript:
144+
```Javascript
145+
var largestSumAfterKNegations = function(nums, k) {
146+
nums.sort((a, b) => {
147+
return Math.abs(b) - Math.abs(a)
148+
})
149+
for(let i = 0; i < nums.length; i++) {
150+
if(nums[i] < 0 && k > 0) {
151+
nums[i] *= -1
152+
k--
153+
}
154+
}
155+
156+
if(k > 0 && k % 2 === 1) {
157+
nums[nums.length - 1] *= -1
158+
}
159+
k = 0
143160

161+
return nums.reduce((a, b) => {
162+
return a + b
163+
})
164+
};
165+
```
144166

145167
-----------------------
146168
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

0 commit comments

Comments
 (0)