Skip to content

Commit

Permalink
Merge pull request youngyangyang04#304 from fusunx/master
Browse files Browse the repository at this point in the history
1005.K次取反.md Javascript
  • Loading branch information
youngyangyang04 authored Jun 1, 2021
2 parents 4e6e98c + 72b2521 commit 7b3f8ea
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions problems/1005.K次取反后最大化的数组和.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,29 @@ class Solution:
Go:


Javascript:
```Javascript
var largestSumAfterKNegations = function(nums, k) {
nums.sort((a, b) => {
return Math.abs(b) - Math.abs(a)
})
for(let i = 0; i < nums.length; i++) {
if(nums[i] < 0 && k > 0) {
nums[i] *= -1
k--
}
}

if(k > 0 && k % 2 === 1) {
nums[nums.length - 1] *= -1
}
k = 0

return nums.reduce((a, b) => {
return a + b
})
};
```

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

0 comments on commit 7b3f8ea

Please sign in to comment.