mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 04:54:51 +08:00
Merge pull request #2035 from sharky7pb/master
1005、K次取反后最大化的数组和,暴力解法,java
This commit is contained in:
@ -153,7 +153,23 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
//暴力解法
|
||||
class Solution {
|
||||
public int largestSumAfterKNegations(int[] nums, int k) {
|
||||
int count = 0;
|
||||
//循环k次
|
||||
for( int i = 0; i < k; i++ ){
|
||||
Arrays.sort( nums );
|
||||
nums[0] = -nums[0];//把最小值换成其相反数
|
||||
}
|
||||
for ( int num : nums ){
|
||||
count += num;//累加
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
```
|
||||
### Python
|
||||
```python
|
||||
class Solution:
|
||||
|
Reference in New Issue
Block a user