Skip to content

Commit 0a6a51b

Browse files
committed
fd
1 parent 36a5454 commit 0a6a51b

File tree

3 files changed

+35
-96
lines changed

3 files changed

+35
-96
lines changed

solution/src/main/java/com/inuker/solution/LongestConsecutiveSequence.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,28 @@
44

55
/**
66
* Created by dingjikerbo on 16/11/22.
7+
* https://leetcode.com/articles/longest-consecutive-sequence/
78
*/
8-
99
public class LongestConsecutiveSequence {
1010

1111
/**
1212
* map中保存的是某个点所在的联通块长度,不过要注意的这个连通块两端的点才是准的,中间的点可能不准
1313
* 所以我们每次新插入一个点时,一定要更新连通块两端的点
1414
*/
1515
public int longestConsecutive(int[] nums) {
16-
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
17-
18-
int res = 0;
19-
20-
for (int i = 0; i < nums.length; i++) {
21-
int n = nums[i];
22-
16+
HashMap<Integer, Integer> map = new HashMap<>();
17+
int longest = 0;
18+
for (int n : nums) {
2319
if (!map.containsKey(n)) {
24-
int left = map.containsKey(n - 1) ? map.get(n - 1) : 0;
25-
int right = map.containsKey(n + 1) ? map.get(n + 1) : 0;
20+
int left = map.getOrDefault(n - 1, 0);
21+
int right = map.getOrDefault(n + 1, 0);
2622
int len = left + right + 1;
27-
28-
// 这句一定不能掉,因为map会查重的,如果这里n没丢到map里,后面再出现重复的n会被覆盖
2923
map.put(n, len);
30-
31-
res = Math.max(res, len);
32-
3324
map.put(n - left, len);
3425
map.put(n + right, len);
26+
longest = Math.max(longest, len);
3527
}
3628
}
37-
38-
return res;
29+
return longest;
3930
}
4031
}

solution/src/main/java/com/inuker/solution/SurroundedRegions.java

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -9,61 +9,6 @@
99

1010
public class SurroundedRegions {
1111

12-
/**
13-
* Union Find法
14-
*/
15-
private int[] mRoot; // union find set
16-
private boolean[] mHasEdge; // whether an union has an 'O' which is on the edge of the matrix
17-
18-
public void solve(char[][] board) {
19-
if (board.length == 0) {
20-
return;
21-
}
22-
23-
// init, every char itself is an union
24-
int row = board.length, col = board[0].length;
25-
mRoot = new int[row * col];
26-
mHasEdge = new boolean[mRoot.length];
27-
for (int i = 0; i < mRoot.length; i++) {
28-
mRoot[i] = i;
29-
}
30-
for (int i = 0; i < mHasEdge.length; i++) {
31-
int x = i / col, y = i % col;
32-
mHasEdge[i] = (board[x][y] == 'O' && (x == 0 || x == row - 1 || y == 0 || y == col - 1));
33-
}
34-
35-
// iterate the matrix, for each char, union it + its upper char + its right char if they equals to each other
36-
for (int i = 0; i < mRoot.length; i++) {
37-
int x = i / col, y = i % col, up = x - 1, right = y + 1;
38-
if (up >= 0 && board[x][y] == board[up][y]) union(i, i - col);
39-
if (right < col && board[x][y] == board[x][right]) union(i, i + 1);
40-
}
41-
42-
// for each char in the matrix, if it is an 'O' and its union doesn't has an 'edge O', the whole union should be setted as 'X'
43-
for (int i = 0; i < mRoot.length; i++) {
44-
int x = i / col, y = i % col;
45-
if (board[x][y] == 'O' && !mHasEdge[find(i)])
46-
board[x][y] = 'X';
47-
}
48-
}
49-
50-
private void union(int x, int y) {
51-
int rootX = find(x);
52-
int rootY = find(y);
53-
// if there is an union has an 'edge O',the union after merge should be marked too
54-
mRoot[rootX] = rootY;
55-
mHasEdge[rootY] = mHasEdge[rootX] || mHasEdge[rootY];
56-
}
57-
58-
private int find(int root) {
59-
if (mRoot[root] == root) {
60-
return root;
61-
}
62-
// 在找的过程中直接联通到根节点了,这样的好处是加速未来的查找
63-
mRoot[root] = find(mRoot[root]);
64-
return mRoot[root];
65-
}
66-
6712
/**
6813
* BFS法
6914
* 给与边界的O相邻的所有O点都标为+,然后剩下的O肯定是不与边界O相邻的,则必然是被X包围的,
@@ -145,7 +90,6 @@ private void dfs(char[][] board, int i, int j) {
14590
}
14691

14792
board[i][j] = '+';
148-
14993
dfs(board, i - 1, j);
15094
dfs(board, i + 1, j);
15195
dfs(board, i, j + 1);

test/src/main/java/com/inuker/test/Test2.java

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,38 @@
2020

2121
public class Test2 {
2222

23-
public int[] findOrder(int numCourses, int[][] prerequisites) {
24-
int[] indegree = new int[numCourses];
25-
Set<Integer>[] sets = new HashSet[numCourses];
26-
for (int i = 0; i < sets.length; i++) {
27-
sets[i] = new HashSet<>();
23+
public void solve(char[][] board) {
24+
if (board.length == 0) {
25+
return;
2826
}
29-
for (int[] pre : prerequisites) {
30-
if (sets[pre[1]].add(pre[0])) {
31-
indegree[pre[0]]++;
32-
}
27+
int row = board.length, col = board[0].length;
28+
for (int i = 0; i < row; i++) {
29+
dfs(board, i, 0);
30+
dfs(board, i, board[0].length - 1);
3331
}
34-
Queue<Integer> queue = new LinkedList<>();
35-
for (int i = 0; i < numCourses; i++) {
36-
if (indegree[i] == 0) {
37-
queue.add(i);
38-
}
32+
for (int i = 0; i < col; i++) {
33+
dfs(board, 0, i);
34+
dfs(board, row - 1, i);
3935
}
40-
int count = 0;
41-
int[] result = new int[numCourses];
42-
while (!queue.isEmpty()) {
43-
Integer n = queue.poll();
44-
result[count++] = n;
45-
for (Integer k : sets[n]) {
46-
if (--indegree[k] == 0) {
47-
queue.add(k);
36+
for (int i = 0; i < row; i++) {
37+
for (int j = 0; j < col; j++) {
38+
if (board[i][j] == 'O') {
39+
board[i][j] = 'X';
40+
} else if (board[i][j] == '#') {
41+
board[i][j] = 'O';
4842
}
4943
}
5044
}
51-
return count == numCourses ? result : new int[0];
45+
}
46+
47+
void dfs(char[][] board, int i, int j) {
48+
if (i < 0 || i >= board.length || j < 0 || j >= board[0].length || board[i][j] != 'O') {
49+
return;
50+
}
51+
board[i][j] = '#';
52+
dfs(board, i + 1, j);
53+
dfs(board, i - 1, j);
54+
dfs(board, i, j + 1);
55+
dfs(board, i, j - 1);
5256
}
5357
}

0 commit comments

Comments
 (0)