Skip to content

Commit 1c001ed

Browse files
committed
fd
1 parent 0a6a51b commit 1c001ed

File tree

4 files changed

+17
-69
lines changed

4 files changed

+17
-69
lines changed

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,10 @@ public List<Integer> numIslands2(int m, int n, int[][] positions) {
2222
List<Integer> list = new LinkedList<Integer>();
2323

2424
mRoots = new int[m * n];
25-
/**
26-
* 首先都指向-1,稍后添加岛时再初始化
27-
*/
2825
Arrays.fill(mRoots, -1);
2926

3027
for (int[] p : positions) {
3128
int x = p[0], y = p[1], z = x * n + y;
32-
/**
33-
* 指向自己
34-
*/
3529
mRoots[z] = z;
3630

3731
mCount++;
@@ -42,9 +36,6 @@ public List<Integer> numIslands2(int m, int n, int[][] positions) {
4236
if (x0 < 0 || x0 >= m || y0 < 0 || y0 >= n || mRoots[z0] == -1) {
4337
continue;
4438
}
45-
/**
46-
* 这里是给z合并到z0中去,即新添加的这个岛要合到老的岛上去
47-
*/
4839
union(z, z0);
4940
}
5041

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

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,19 @@ public class ShortestWordDistance {
1111

1212
// 耗时3ms
1313
public int shortestDistance(String[] words, String word1, String word2) {
14-
List<Integer> indexs1 = new LinkedList<Integer>();
15-
List<Integer> indexs2 = new LinkedList<Integer>();
14+
int l1 = -1, l2 = -1, shortest = Integer.MAX_VALUE;
1615
for (int i = 0; i < words.length; i++) {
1716
if (words[i].equals(word1)) {
18-
indexs1.add(i);
17+
l1 = i;
1918
} else if (words[i].equals(word2)) {
20-
indexs2.add(i);
21-
}
22-
}
23-
24-
int size1 = indexs1.size(), size2 = indexs2.size();
25-
int min = Math.abs(indexs1.get(size1 - 1) - indexs2.get(size2 - 1));
26-
for (int i = 0, j = 0; i < size1 && j < size2; ) {
27-
int index1 = indexs1.get(i);
28-
int index2 = indexs2.get(j);
29-
30-
min = Math.min(min, Math.abs(index1 - index2));
31-
32-
if (index1 < index2) {
33-
i++;
34-
} else if (index1 > index2) {
35-
j++;
19+
l2 = i;
3620
} else {
37-
return 0;
21+
continue;
22+
}
23+
if (l1 >= 0 && l2 >= 0) {
24+
shortest = Math.min(shortest, Math.abs(l1 - l2));
3825
}
3926
}
40-
41-
return min;
27+
return shortest;
4228
}
4329
}

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.ArrayList;
44
import java.util.HashMap;
5+
import java.util.LinkedList;
56
import java.util.List;
67
import java.util.Map;
78

@@ -16,14 +17,8 @@ public class ShortestWordDistanceII {
1617
public ShortestWordDistanceII(String[] words) {
1718
map = new HashMap<String, List<Integer>>();
1819
for (int i = 0; i < words.length; i++) {
19-
String w = words[i];
20-
if (map.containsKey(w)) {
21-
map.get(w).add(i);
22-
} else {
23-
List<Integer> list = new ArrayList<Integer>();
24-
list.add(i);
25-
map.put(w, list);
26-
}
20+
List<Integer> list = map.computeIfAbsent(words[i], k -> new LinkedList<>());
21+
list.add(i);
2722
}
2823
}
2924

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

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,14 @@
2020

2121
public class Test2 {
2222

23-
public void solve(char[][] board) {
24-
if (board.length == 0) {
25-
return;
26-
}
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);
31-
}
32-
for (int i = 0; i < col; i++) {
33-
dfs(board, 0, i);
34-
dfs(board, row - 1, i);
35-
}
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';
42-
}
43-
}
23+
class WordDistance {
24+
25+
public WordDistance(String[] words) {
26+
HashMap<String, List<>>
4427
}
45-
}
4628

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;
29+
public int shortest(String word1, String word2) {
30+
5031
}
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);
5632
}
5733
}

0 commit comments

Comments
 (0)