mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #686 from ironartisan/master
修复1005.K取反后最大化的数组和Java代码K书写错误问题
This commit is contained in:
@ -183,6 +183,32 @@ class Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
```java
|
||||||
|
// 解法2:通过判断path中是否存在数字,排除已经选择的数字
|
||||||
|
class Solution {
|
||||||
|
List<List<Integer>> result = new ArrayList<>();
|
||||||
|
LinkedList<Integer> path = new LinkedList<>();
|
||||||
|
public List<List<Integer>> permute(int[] nums) {
|
||||||
|
if (nums.length == 0) return result;
|
||||||
|
backtrack(nums, path);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
public void backtrack(int[] nums, LinkedList<Integer> path) {
|
||||||
|
if (path.size() == nums.length) {
|
||||||
|
result.add(new ArrayList<>(path));
|
||||||
|
}
|
||||||
|
for (int i =0; i < nums.length; i++) {
|
||||||
|
// 如果path中已有,则跳过
|
||||||
|
if (path.contains(nums[i])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
path.add(nums[i]);
|
||||||
|
backtrack(nums, path);
|
||||||
|
path.removeLast();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
```python3
|
```python3
|
||||||
|
@ -157,6 +157,28 @@ class Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
```java
|
||||||
|
// 版本2
|
||||||
|
class Solution {
|
||||||
|
public int[][] merge(int[][] intervals) {
|
||||||
|
LinkedList<int[]> res = new LinkedList<>();
|
||||||
|
Arrays.sort(intervals, (o1, o2) -> Integer.compare(o1[0], o2[0]));
|
||||||
|
res.add(intervals[0]);
|
||||||
|
for (int i = 1; i < intervals.length; i++) {
|
||||||
|
if (intervals[i][0] <= res.getLast()[1]) {
|
||||||
|
int start = res.getLast()[0];
|
||||||
|
int end = Math.max(intervals[i][1], res.getLast()[1]);
|
||||||
|
res.removeLast();
|
||||||
|
res.add(new int[]{start, end});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
res.add(intervals[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res.toArray(new int[res.size()][]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
```python
|
```python
|
||||||
|
@ -200,6 +200,7 @@ public:
|
|||||||
|
|
||||||
Java:
|
Java:
|
||||||
```java
|
```java
|
||||||
|
// 解法1
|
||||||
class Solution {
|
class Solution {
|
||||||
public int canCompleteCircuit(int[] gas, int[] cost) {
|
public int canCompleteCircuit(int[] gas, int[] cost) {
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
@ -221,7 +222,26 @@ class Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
```java
|
||||||
|
// 解法2
|
||||||
|
class Solution {
|
||||||
|
public int canCompleteCircuit(int[] gas, int[] cost) {
|
||||||
|
int curSum = 0;
|
||||||
|
int totalSum = 0;
|
||||||
|
int index = 0;
|
||||||
|
for (int i = 0; i < gas.length; i++) {
|
||||||
|
curSum += gas[i] - cost[i];
|
||||||
|
totalSum += gas[i] - cost[i];
|
||||||
|
if (curSum < 0) {
|
||||||
|
index = (i + 1) % gas.length ;
|
||||||
|
curSum = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (totalSum < 0) return -1;
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
Python:
|
Python:
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
|
@ -110,15 +110,16 @@ class Solution {
|
|||||||
int len = nums.length;
|
int len = nums.length;
|
||||||
for (int i = 0; i < len; i++) {
|
for (int i = 0; i < len; i++) {
|
||||||
//从前向后遍历,遇到负数将其变为正数,同时K--
|
//从前向后遍历,遇到负数将其变为正数,同时K--
|
||||||
if (nums[i] < 0 && k > 0) {
|
if (nums[i] < 0 && K > 0) {
|
||||||
nums[i] = -nums[i];
|
nums[i] = -nums[i];
|
||||||
k--;
|
K--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 如果K还大于0,那么反复转变数值最小的元素,将K用完
|
// 如果K还大于0,那么反复转变数值最小的元素,将K用完
|
||||||
if (k % 2 == 1) nums[len - 1] = -nums[len - 1];
|
|
||||||
|
|
||||||
|
if (K % 2 == 1) nums[len - 1] = -nums[len - 1];
|
||||||
return Arrays.stream(nums).sum();
|
return Arrays.stream(nums).sum();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Reference in New Issue
Block a user