Skip to content

Commit 57475dd

Browse files
author
Botao Xiao
committed
[Function add]: 1. Add leetcode solutions with tag graph.
1 parent 2b373c6 commit 57475dd

11 files changed

+631
-53
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,14 @@
618618
* [576. Out of Boundary Paths](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/576.%20Out%20of%20Boundary%20Paths.md)
619619
* [935. Knight Dialer](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/935.%20Knight%20Dialer.md)
620620

621+
#### Graph
622+
* [133. Clone Graph](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/133.%20Clone%20Graph.md)
623+
* [138. Copy List with Random Pointer](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/138.%20Copy%20List%20with%20Random%20Pointer.md)
624+
* [200. Number of Islands](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/200.%20Number%20of%20Islands.md)
625+
* [547. Friend Circles](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/547.%20Friend%20Circles.md)
626+
* [695. Max Area of Island](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/695.%20Max%20Area%20of%20Island.md)
627+
* [733. Flood Fill](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/733.%20Flood%20Fill.md)
628+
621629
## Algorithm(4th_Edition)
622630
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
623631
All java realization codes are placed in different packages.

leetcode/10. Regular Expression Matching.md

+33-1
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,36 @@ class Solution {
154154
return c1 == c2 || c2 == '.';
155155
}
156156
}
157-
```
157+
```
158+
159+
### Fourth time
160+
* Method 1: dp
161+
```Java
162+
class Solution {
163+
public boolean isMatch(String s, String p) {
164+
char[] sArr = s.toCharArray();
165+
char[] pArr = p.toCharArray();
166+
int sLen = sArr.length, pLen = pArr.length;
167+
boolean[][] dp = new boolean[sLen + 1][pLen + 1];
168+
dp[0][0] = true;
169+
for(int i = 1; i <= pLen; i++){
170+
if(pArr[i - 1] == '*')
171+
dp[0][i] |= (i - 2 >= 0 ? dp[0][i - 2]: false);
172+
}
173+
for(int i = 1; i <= sLen; i++){
174+
for(int j = 1; j <= pLen; j++){
175+
if(match(sArr[i - 1], pArr[j - 1])) dp[i][j] |= dp[i - 1][j - 1];
176+
else if(pArr[j - 1] == '*'){
177+
dp[i][j] |= (j - 2 >= 0 ? dp[i][j - 2]: false);
178+
if(j > 1 && match(sArr[i - 1], pArr[j - 2]))
179+
dp[i][j] |= dp[i - 1][j];
180+
}
181+
}
182+
}
183+
return dp[sLen][pLen];
184+
}
185+
private boolean match(char a, char b){
186+
return a == b || b == '.';
187+
}
188+
}
189+
```

leetcode/133. Clone Graph.md

+42-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class Solution {
5656
```
5757

5858
### 二刷
59-
1. 参考了一刷,使用BFS
59+
1. 参考了一刷,使用DFS
6060
```Java
6161
/**
6262
* Definition for undirected graph.
@@ -83,3 +83,44 @@ public class Solution {
8383
}
8484
}
8585
```
86+
87+
### Third Time
88+
* Method 1: dfs + map
89+
* Step 1: Check is current node is empty.
90+
* Step 2: Check if cached.
91+
* Step 3: Create a new node and save it into map.
92+
* Step 4: Use dfs to copy all its neighbours.
93+
```Java
94+
/*
95+
// Definition for a Node.
96+
class Node {
97+
public int val;
98+
public List<Node> neighbors;
99+
public Node() {}
100+
public Node(int _val,List<Node> _neighbors) {
101+
val = _val;
102+
neighbors = _neighbors;
103+
}
104+
};
105+
*/
106+
class Solution {
107+
private Map<Integer, Node> map;
108+
public Node cloneGraph(Node node) {
109+
map = new HashMap<>();
110+
return dfs(node, node.val);
111+
}
112+
private Node dfs(Node node, int num){
113+
if(node == null) return null;
114+
else if(map.containsKey(num)) return map.get(num);
115+
else{
116+
List<Node> neighbours = new ArrayList<>();
117+
Node cur = new Node(num, neighbours);
118+
map.put(num, cur);
119+
for(Node neighbour : node.neighbors){
120+
neighbours.add(dfs(neighbour, neighbour.val));
121+
}
122+
return cur;
123+
}
124+
}
125+
}
126+
```

leetcode/138. Copy List with Random Pointer.md

+43-1
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,46 @@ public class Solution {
125125
return result.next;
126126
}
127127
}
128-
```
128+
```
129+
130+
### Third Time
131+
* Method 1: dfs + map
132+
* This question is a graph question, for each nodes, we can consider its next and random as two neighbours.
133+
* Related question [133. Clone Graph](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/133.%20Clone%20Graph.md)
134+
```Java
135+
/*
136+
// Definition for a Node.
137+
class Node {
138+
public int val;
139+
public Node next;
140+
public Node random;
141+
142+
public Node() {}
143+
144+
public Node(int _val,Node _next,Node _random) {
145+
val = _val;
146+
next = _next;
147+
random = _random;
148+
}
149+
};
150+
*/
151+
class Solution {
152+
private Map<Integer, Node> map;
153+
public Node copyRandomList(Node head) {
154+
map = new HashMap<>();
155+
return head == null ? null: dfs(head, head.val);
156+
}
157+
private Node dfs(Node node, int num){
158+
if(node == null) return null;
159+
else if(map.containsKey(num)) return map.get(num);
160+
else{
161+
Node cur = new Node();
162+
cur.val = num;
163+
map.put(num, cur);
164+
cur.next = node.next == null ? null: dfs(node.next, node.next.val);
165+
cur.random = node.random == null ? null: dfs(node.random, node.random.val);
166+
return cur;
167+
}
168+
}
169+
}
170+
```

leetcode/200. Number of Islands.md

+88
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,91 @@ class Solution {
132132
}
133133
}
134134
```
135+
136+
### Third Time
137+
* Method 1: dfs
138+
```Java
139+
class Solution {
140+
private int[][] dir = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
141+
private int height;
142+
private int width;
143+
public int numIslands(char[][] grid) {
144+
if(grid == null || grid.length == 0 || grid[0].length == 0) return 0;
145+
this.height = grid.length;
146+
this.width = grid[0].length;
147+
int res = 0;
148+
for(int i = 0; i < height; i++){
149+
for(int j = 0; j < width; j++){
150+
if(grid[i][j] == '0') continue;
151+
dfs(grid, i, j);
152+
res++;
153+
}
154+
}
155+
return res;
156+
}
157+
private void dfs(char[][] grid, int row, int col){
158+
grid[row][col] = '0';
159+
int tx = 0, ty = 0;
160+
for(int i = 0; i < 4; i++){
161+
tx = row + dir[i][0];
162+
ty = col + dir[i][1];
163+
if(tx >= 0 && tx < height && ty >= 0 && ty < width && grid[tx][ty] != '0')
164+
dfs(grid, tx, ty);
165+
}
166+
}
167+
}
168+
```
169+
170+
* Method 2: union-find
171+
1. For any single node, we can check its right side node and bottom side node and add them to union find set.
172+
2. There are two tricks for optimize union find set:
173+
* path compression.
174+
* merge using rank.
175+
```Java
176+
class Solution {
177+
private int[] uf;
178+
private static int[][] dir = new int[][]{{0, 1}, {1, 0}};
179+
public int numIslands(char[][] grid) {
180+
if(grid == null || grid.length == 0 || grid[0].length == 0) return 0;
181+
int height = grid.length;
182+
int width = grid[0].length;
183+
uf = new int[height * width];
184+
for(int i = 0; i < height; i++){
185+
for(int j = 0; j < width; j++){
186+
if(grid[i][j] == '1')
187+
uf[i * width + j] = i * width + j;
188+
else uf[i * width + j] = -1;
189+
}
190+
}
191+
for(int i = 0; i < height; i++){
192+
for(int j = 0; j < width; j++){
193+
if(grid[i][j] == '0') continue;
194+
int tx = 0, ty = 0;
195+
for(int d = 0; d < 2; d++){
196+
tx = i + dir[d][0];
197+
ty = j + dir[d][1];
198+
if(tx >= 0 && tx < height && ty >= 0 && ty < width && grid[tx][ty] == '1'){
199+
union(i * width + j, tx * width + ty);
200+
}
201+
}
202+
}
203+
}
204+
int res = 0;
205+
for(int i = 0; i < uf.length; i++){
206+
if(uf[i] == i) res++;
207+
}
208+
return res;
209+
}
210+
private void union(int i, int j){
211+
int p = find(i);
212+
int q = find(j);
213+
uf[p] = q;
214+
}
215+
private int find(int i){
216+
if(uf[i] != i){
217+
uf[i] = find(uf[i]);
218+
}
219+
return uf[i];
220+
}
221+
}
222+
```

leetcode/44. Wildcard Matching.md

+30-1
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,33 @@ class Solution {
8888
return a == b || b == '?';
8989
}
9090
}
91-
```
91+
```
92+
93+
### Third Time
94+
* Method 1: dp
95+
```Java
96+
class Solution {
97+
public boolean isMatch(String s, String p) {
98+
char[] sArr = s.toCharArray();
99+
char[] pArr = p.toCharArray();
100+
int sLen = sArr.length, pLen = pArr.length;
101+
boolean[][] dp = new boolean[sLen + 1][pLen + 1];
102+
dp[0][0] = true;
103+
for(int i = 1; i <= pLen; i++)
104+
if(pArr[i - 1] == '*')
105+
dp[0][i] = dp[0][i - 1];
106+
for(int i = 1; i <= sLen; i++){
107+
for(int j = 1; j <= pLen; j++){
108+
if(match(sArr[i - 1], pArr[j - 1])) dp[i][j] |= dp[i - 1][j - 1];
109+
else if(pArr[j - 1] == '*'){
110+
dp[i][j] |= dp[i - 1][j] || dp[i - 1][j - 1] || dp[i][j - 1];
111+
}
112+
}
113+
}
114+
return dp[sLen][pLen];
115+
}
116+
private boolean match(char a, char b){
117+
return a == b || b == '?';
118+
}
119+
}
120+
```

leetcode/547. Friend Circles.md

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
## 547. Friend Circles
2+
3+
### Question
4+
There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct friend of C, then A is an indirect friend of C. And we defined a friend circle is a group of students who are direct or indirect friends.
5+
6+
Given a N*N matrix M representing the friend relationship between students in the class. If M[i][j] = 1, then the ith and jth students are direct friends with each other, otherwise not. And you have to output the total number of friend circles among all the students.
7+
8+
```
9+
Example 1:
10+
Input:
11+
[[1,1,0],
12+
[1,1,0],
13+
[0,0,1]]
14+
Output: 2
15+
Explanation:The 0th and 1st students are direct friends, so they are in a friend circle.
16+
The 2nd student himself is in a friend circle. So return 2.
17+
Example 2:
18+
Input:
19+
[[1,1,0],
20+
[1,1,1],
21+
[0,1,1]]
22+
Output: 1
23+
Explanation:The 0th and 1st students are direct friends, the 1st and 2nd students are direct friends,
24+
so the 0th and 2nd students are indirect friends. All of them are in the same friend circle, so return 1.
25+
```
26+
27+
Note:
28+
* N is in range [1,200].
29+
* M[i][i] = 1 for all students.
30+
* If M[i][j] = 1, then M[j][i] = 1.
31+
32+
### Solution:
33+
* Method 1:Union find
34+
```Java
35+
class Solution {
36+
private int[] uf;
37+
private int num;
38+
public int findCircleNum(int[][] M) {
39+
if(M == null || M.length == 0) return 0;
40+
this.num = M.length;
41+
uf = new int[num];
42+
for(int i = 0; i < num; i++) uf[i] = i;
43+
for(int i = 0; i < num; i++){
44+
for(int j = i + 1; j < num; j++){
45+
if(M[i][j] == 1){ //means student i and j are friends
46+
union(i, j);
47+
}
48+
}
49+
}
50+
int res = 0;
51+
for(int i = 0; i < uf.length; i++){
52+
if(uf[i] == i)
53+
res++;
54+
}
55+
return res;
56+
}
57+
private void union(int i, int j){
58+
int p = find(i);
59+
int q = find(j);
60+
if(p != q){
61+
uf[p] = q;
62+
}
63+
}
64+
private int find(int i){
65+
if(uf[i] != i){
66+
uf[i] = find(uf[i]);
67+
}
68+
return uf[i];
69+
}
70+
}
71+
```
72+
73+
* Method 2: DFS
74+
* Once we get a not visited student, we set all its friends(including himself) as visited.
75+
```Java
76+
class Solution {
77+
private boolean[] visited;
78+
private int num;
79+
private int res;
80+
public int findCircleNum(int[][] M) {
81+
if(M == null || M.length == 0) return 0;
82+
this.num = M.length;
83+
this.visited = new boolean[num];
84+
for(int i = 0; i < num; i++){
85+
if(!visited[i]){
86+
dfs(M, i);
87+
res++;
88+
}
89+
}
90+
return res;
91+
}
92+
private void dfs(int[][] M, int i){
93+
visited[i] = true;
94+
for(int j = 0; j < num; j++){
95+
if(!visited[j] && j != i && M[i][j] == 1){
96+
dfs(M, j);
97+
}
98+
}
99+
}
100+
}
101+
```

0 commit comments

Comments
 (0)