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 5043002 commit 72b2521Copy full SHA for 72b2521
problems/1005.K次取反后最大化的数组和.md
@@ -140,7 +140,29 @@ class Solution:
140
Go:
141
142
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
160
161
+ return nums.reduce((a, b) => {
162
+ return a + b
163
164
+};
165
+```
166
167
-----------------------
168
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
0 commit comments