mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2026-02-02 18:39:09 +08:00
Merge branch 'master' into master
This commit is contained in:
@@ -211,6 +211,7 @@ var largestSumAfterKNegations = function(nums, k) {
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
### C
|
||||
```c
|
||||
#define abs(a) (((a) > 0) ? (a) : (-(a)))
|
||||
@@ -250,5 +251,30 @@ int largestSumAfterKNegations(int* nums, int numsSize, int k){
|
||||
}
|
||||
```
|
||||
|
||||
### TypeScript
|
||||
|
||||
```typescript
|
||||
function largestSumAfterKNegations(nums: number[], k: number): number {
|
||||
nums.sort((a, b) => Math.abs(b) - Math.abs(a));
|
||||
let curIndex: number = 0;
|
||||
const length = nums.length;
|
||||
while (curIndex < length && k > 0) {
|
||||
if (nums[curIndex] < 0) {
|
||||
nums[curIndex] *= -1;
|
||||
k--;
|
||||
}
|
||||
curIndex++;
|
||||
}
|
||||
while (k > 0) {
|
||||
nums[length - 1] *= -1;
|
||||
k--;
|
||||
}
|
||||
return nums.reduce((pre, cur) => pre + cur, 0);
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
||||
Reference in New Issue
Block a user