mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 20:40:39 +08:00
1005、K次取反后最大化的数组和,暴力解法,java
This commit is contained in:
@ -145,7 +145,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
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
|
Reference in New Issue
Block a user