auto commit

This commit is contained in:
CyC2018 2018-06-16 11:02:10 +08:00
parent 3c1496ef64
commit a532e92950

View File

@ -4588,12 +4588,18 @@ For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums sh
```java ```java
public void moveZeroes(int[] nums) { public void moveZeroes(int[] nums) {
int idx = 0; int idx = 0;
for (int num : nums) if (num != 0) nums[idx++] = num; for (int num : nums) {
while (idx < nums.length) nums[idx++] = 0; if (num != 0) {
nums[idx++] = num;
}
}
while (idx < nums.length) {
nums[idx++] = 0;
}
} }
``` ```
**调整矩阵** **改变矩阵维度**
[566. Reshape the Matrix (Easy)](https://leetcode.com/problems/reshape-the-matrix/description/) [566. Reshape the Matrix (Easy)](https://leetcode.com/problems/reshape-the-matrix/description/)
@ -4603,8 +4609,10 @@ nums =
[[1,2], [[1,2],
[3,4]] [3,4]]
r = 1, c = 4 r = 1, c = 4
Output: Output:
[[1,2,3,4]] [[1,2,3,4]]
Explanation: Explanation:
The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list. The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list.
``` ```
@ -4612,16 +4620,18 @@ The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matr
```java ```java
public int[][] matrixReshape(int[][] nums, int r, int c) { public int[][] matrixReshape(int[][] nums, int r, int c) {
int m = nums.length, n = nums[0].length; int m = nums.length, n = nums[0].length;
if (m * n != r * c) return nums; if (m * n != r * c) {
int[][] ret = new int[r][c]; return nums;
}
int[][] reshapedNums = new int[r][c];
int index = 0; int index = 0;
for (int i = 0; i < r; i++) { for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) { for (int j = 0; j < c; j++) {
ret[i][j] = nums[index / n][index % n]; reshapedNums[i][j] = nums[index / n][index % n];
index++; index++;
} }
} }
return ret; return reshapedNums;
} }
``` ```
@ -4632,15 +4642,15 @@ public int[][] matrixReshape(int[][] nums, int r, int c) {
```java ```java
public int findMaxConsecutiveOnes(int[] nums) { public int findMaxConsecutiveOnes(int[] nums) {
int max = 0, cur = 0; int max = 0, cur = 0;
for (int num : nums) { for (int x : nums) {
cur = num == 0 ? 0 : cur + 1; cur = x == 0 ? 0 : cur + 1;
max = Math.max(max, cur); max = Math.max(max, cur);
} }
return max; return max;
} }
``` ```
**一个数组元素在 [1, n] 之间,其中一个数被替换为另一个数,找出丢失的数和重复的数** **一个数组元素在 [1, n] 之间,其中一个数被替换为另一个数,找出重复的数和丢失的数**
[645. Set Mismatch (Easy)](https://leetcode.com/problems/set-mismatch/description/) [645. Set Mismatch (Easy)](https://leetcode.com/problems/set-mismatch/description/)
@ -4656,24 +4666,27 @@ Output: [2,3]
最直接的方法是先对数组进行排序,这种方法时间复杂度为 O(NlogN)。本题可以以 O(N) 的时间复杂度、O(1) 空间复杂度来求解。 最直接的方法是先对数组进行排序,这种方法时间复杂度为 O(NlogN)。本题可以以 O(N) 的时间复杂度、O(1) 空间复杂度来求解。
主要思想是通过交换数组元素,使得数组上的元素在正确的位置上。遍历数组,如果第 i 位上的元素不是 i + 1那么就交换第 i 位和 nums[i] - 1 位上的元素,使得 num[i] - 1 位置上的元素为 nums[i],也就是该位置上的元素是正确的 主要思想是通过交换数组元素,使得数组上的元素在正确的位置上。遍历数组,如果第 i 位上的元素不是 i + 1那么一直交换第 i 位和 nums[i] - 1 位置上的元素
```java ```java
public int[] findErrorNums(int[] nums) { public int[] findErrorNums(int[] nums) {
for (int i = 0; i < nums.length; i++) { for (int i = 0; i < nums.length; i++) {
while (nums[i] != i + 1) { while (nums[i] != i + 1 && nums[nums[i] - 1] != nums[i]) {
if (nums[i] == nums[nums[i] - 1]) {
return new int[]{nums[nums[i] - 1], i + 1};
}
swap(nums, i, nums[i] - 1); swap(nums, i, nums[i] - 1);
} }
} }
for (int i = 0; i < nums.length; i++) {
if (nums[i] != i + 1) {
return new int[]{nums[i], i + 1};
}
}
return null; return null;
} }
private void swap(int[] nums, int i, int j) { private void swap(int[] nums, int i, int j) {
int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp; int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
} }
``` ```
@ -6408,22 +6421,24 @@ We cannot find a way to divide the set of nodes into two independent subsets.
public boolean isBipartite(int[][] graph) { public boolean isBipartite(int[][] graph) {
int[] colors = new int[graph.length]; int[] colors = new int[graph.length];
Arrays.fill(colors, -1); Arrays.fill(colors, -1);
for (int i = 0; i < graph.length; i++) { for (int i = 0; i < graph.length; i++) { // 处理图不是连通的情况
if (colors[i] == -1 && !isBipartite(graph, i, 0, colors)) if (colors[i] == -1 && !isBipartite(i, 0, colors, graph)) {
return false; return false;
}
} }
return true; return true;
} }
private boolean isBipartite(int[][] graph, int node, int color, int[] colors) { private boolean isBipartite(int curNode, int curColor, int[] colors, int[][] graph) {
if (colors[node] != -1) if (colors[curNode] != -1) {
return colors[node] == color; return colors[curNode] == curColor;
}
colors[node] = color; colors[curNode] = curColor;
for (int next : graph[node]) for (int nextNode : graph[curNode]) {
if (!isBipartite(graph, next, 1 - color, colors)) if (!isBipartite(nextNode, 1 - curColor, colors, graph)) {
return false; return false;
}
}
return true; return true;
} }
``` ```
@ -6453,36 +6468,40 @@ return false
```java ```java
public boolean canFinish(int numCourses, int[][] prerequisites) { public boolean canFinish(int numCourses, int[][] prerequisites) {
List<Integer>[] graphic = new List[numCourses]; List<Integer>[] graphic = new List[numCourses];
for (int i = 0; i < numCourses; i++) for (int i = 0; i < numCourses; i++) {
graphic[i] = new ArrayList<>(); graphic[i] = new ArrayList<>();
for (int[] pre : prerequisites) }
for (int[] pre : prerequisites) {
graphic[pre[0]].add(pre[1]); graphic[pre[0]].add(pre[1]);
}
boolean[] globalMarked = new boolean[numCourses]; boolean[] globalMarked = new boolean[numCourses];
boolean[] localMarked = new boolean[numCourses]; boolean[] localMarked = new boolean[numCourses];
for (int i = 0; i < numCourses; i++) for (int i = 0; i < numCourses; i++) {
if (!dfs(globalMarked, localMarked, graphic, i)) if (hasCycle(globalMarked, localMarked, graphic, i)) {
return false; return false;
}
}
return true; return true;
} }
private boolean dfs(boolean[] globalMarked, boolean[] localMarked, List<Integer>[] graphic, int curNode) { private boolean hasCycle(boolean[] globalMarked, boolean[] localMarked,
if (localMarked[curNode]) List<Integer>[] graphic, int curNode) {
return false;
if (globalMarked[curNode])
return true;
if (localMarked[curNode]) {
return true;
}
if (globalMarked[curNode]) {
return false;
}
globalMarked[curNode] = true; globalMarked[curNode] = true;
localMarked[curNode] = true; localMarked[curNode] = true;
for (int nextNode : graphic[curNode]) {
for (int nextNode : graphic[curNode]) if (hasCycle(globalMarked, localMarked, graphic, nextNode)) {
if (!dfs(globalMarked, localMarked, graphic, nextNode)) return true;
return false; }
}
localMarked[curNode] = false; localMarked[curNode] = false;
return false;
return true;
} }
``` ```
@ -6495,48 +6514,53 @@ private boolean dfs(boolean[] globalMarked, boolean[] localMarked, List<Integer>
There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]. Another correct ordering is[0,2,1,3]. There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]. Another correct ordering is[0,2,1,3].
``` ```
使用 DFS 来实现拓扑排序,使用一个栈存储后序遍历结果,这个栈元素的逆序结果就是拓扑排序结果。 使用 DFS 来实现拓扑排序,使用一个栈存储后序遍历结果,这个栈的逆序结果就是拓扑排序结果。
证明对于任何先序关系v->w后序遍历结果可以保证 w 先进入栈中,因此栈的逆序结果中 v 会在 w 之前。 证明对于任何先序关系v->w后序遍历结果可以保证 w 先进入栈中,因此栈的逆序结果中 v 会在 w 之前。
```java ```java
public int[] findOrder(int numCourses, int[][] prerequisites) { public int[] findOrder(int numCourses, int[][] prerequisites) {
List<Integer>[] graphic = new List[numCourses]; List<Integer>[] graphic = new List[numCourses];
for (int i = 0; i < numCourses; i++) for (int i = 0; i < numCourses; i++) {
graphic[i] = new ArrayList<>(); graphic[i] = new ArrayList<>();
for (int[] pre : prerequisites) }
for (int[] pre : prerequisites) {
graphic[pre[0]].add(pre[1]); graphic[pre[0]].add(pre[1]);
}
Stack<Integer> topologyOrder = new Stack<>(); Stack<Integer> postOrder = new Stack<>();
boolean[] globalMarked = new boolean[numCourses]; boolean[] globalMarked = new boolean[numCourses];
boolean[] localMarked = new boolean[numCourses]; boolean[] localMarked = new boolean[numCourses];
for (int i = 0; i < numCourses; i++) for (int i = 0; i < numCourses; i++) {
if (!dfs(globalMarked, localMarked, graphic, i, topologyOrder)) if (hasCycle(globalMarked, localMarked, graphic, i, postOrder)) {
return new int[0]; return new int[0];
}
int[] ret = new int[numCourses]; }
for (int i = numCourses - 1; i >= 0; i--) int[] orders = new int[numCourses];
ret[i] = topologyOrder.pop(); for (int i = numCourses - 1; i >= 0; i--) {
return ret; orders[i] = postOrder.pop();
}
return orders;
} }
private boolean dfs(boolean[] globalMarked, boolean[] localMarked, List<Integer>[] graphic, int curNode, Stack<Integer> topologyOrder) { private boolean hasCycle(boolean[] globalMarked, boolean[] localMarked, List<Integer>[] graphic,
if (localMarked[curNode]) int curNode, Stack<Integer> postOrder) {
return false;
if (globalMarked[curNode])
return true;
if (localMarked[curNode]) {
return true;
}
if (globalMarked[curNode]) {
return false;
}
globalMarked[curNode] = true; globalMarked[curNode] = true;
localMarked[curNode] = true; localMarked[curNode] = true;
for (int nextNode : graphic[curNode]) {
for (int nextNode : graphic[curNode]) if (hasCycle(globalMarked, localMarked, graphic, nextNode, postOrder)) {
if (!dfs(globalMarked, localMarked, graphic, nextNode, topologyOrder)) return true;
return false; }
}
localMarked[curNode] = false; localMarked[curNode] = false;
topologyOrder.push(curNode); postOrder.push(curNode);
return false;
return true;
} }
``` ```
@ -6559,15 +6583,13 @@ Explanation: The given undirected graph will be like this:
题目描述:有一系列的边连成的图,找出一条边,移除它之后该图能够成为一棵树。 题目描述:有一系列的边连成的图,找出一条边,移除它之后该图能够成为一棵树。
使用 Union-Find。
```java ```java
public int[] findRedundantConnection(int[][] edges) { public int[] findRedundantConnection(int[][] edges) {
int N = edges.length; int N = edges.length;
UF uf = new UF(N); UF uf = new UF(N);
for (int[] e : edges) { for (int[] e : edges) {
int u = e[0], v = e[1]; int u = e[0], v = e[1];
if (uf.find(u) == uf.find(v)) { if (uf.connect(u, v)) {
return e; return e;
} }
uf.union(u, v); uf.union(u, v);
@ -6576,7 +6598,7 @@ public int[] findRedundantConnection(int[][] edges) {
} }
private class UF { private class UF {
int[] id; private int[] id;
UF(int N) { UF(int N) {
id = new int[N + 1]; id = new int[N + 1];
@ -6601,6 +6623,10 @@ private class UF {
int find(int p) { int find(int p) {
return id[p]; return id[p];
} }
boolean connect(int u, int v) {
return find(u) == find(v);
}
} }
``` ```