Skip to content

Commit 5b122fa

Browse files
author
Botao Xiao
committed
[Function add]: 1. Add leetcode solutions with tag search and dfs.
1 parent 89c632c commit 5b122fa

16 files changed

+211
-8
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,15 @@
538538
* [943. Find the Shortest Superstring](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/943.%20Find%20the%20Shortest%20Superstring.md)
539539
* [996. Number of Squareful Arrays](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/996.%20Number%20of%20Squareful%20Arrays.md)
540540

541+
#### DFS
542+
* [22. Generate Parentheses](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/22.%20Generate%20Parentheses.md)
543+
* [301. Remove Invalid Parentheses](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/301.%20Remove%20Invalid%20Parentheses.md)
544+
* [37. Sudoku Solver](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/37.%20Sudoku%20Solver.md)
545+
* [51. N-Queens](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/51.%20N-Queens.md)
546+
* [52. N-Queens II](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/52.%20N-Queens%20II.md)
547+
* [79. Word Search](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/79.%20Word%20Search.md)
548+
* [[212. Word Search II](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/212.%20Word%20Search%20II.md)]
549+
541550
## Algorithm(4th_Edition)
542551
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
543552
All java realization codes are placed in different packages.

leetcode/212. Word Search II.md

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Each word must be constructed from letters of sequentially adjacent cell, where
88
```
99
Example:
1010
11-
Input:
11+
Input:
1212
words = ["oath","pea","eat","rain"] and board =
1313
[
1414
['o','a','a','n'],
@@ -156,4 +156,46 @@ class Solution {
156156
return false;
157157
}
158158
}
159-
```
159+
```
160+
161+
### Third time
162+
```Java
163+
class Solution {
164+
public List<String> findWords(char[][] board, String[] words) {
165+
List<String> result = new ArrayList<>();
166+
if(words == null || words.length == 0) return result;
167+
int height = board.length, width = board[0].length;
168+
boolean[][] visited = new boolean[height][width];
169+
Set<String> set = new HashSet<>();
170+
for(String word: words){
171+
for(int i = 0; i < height; i++){
172+
for(int j = 0; j < width; j++){
173+
if(board[i][j] == word.charAt(0)){
174+
visited[i][j] = true;
175+
dfs(board, set, word, i, j, 0, new boolean[height][width]);
176+
visited[i][j] = false;
177+
}
178+
}
179+
}
180+
}
181+
result.addAll(set);
182+
return result;
183+
}
184+
int[][] dir = new int[][]{{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
185+
private void dfs(char[][] board, Set<String> result, String word, int i, int j, int index, boolean[][] visited){
186+
if(index == word.length() - 1){
187+
result.add(word);
188+
}else{
189+
visited[i][j] = true;
190+
int tempRow = 0, tempCol = 0;
191+
for(int d = 0; d < 4; d++){
192+
tempRow = i + dir[d][0];
193+
tempCol = j + dir[d][1];
194+
if(tempRow >= 0 && tempRow < visited.length && tempCol >= 0 && tempCol < visited[0].length && !visited[tempRow][tempCol] && board[tempRow][tempCol] == word.charAt(index + 1))
195+
dfs(board, result, word, tempRow, tempCol, index + 1, visited);
196+
}
197+
visited[i][j] = false;
198+
}
199+
}
200+
}
201+
```

leetcode/46. Permutations.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,29 @@ class Solution {
7272
}
7373
}
7474
}
75-
```
75+
```
76+
77+
### Third time
78+
```Java
79+
class Solution {
80+
public List<List<Integer>> permute(int[] nums) {
81+
List<List<Integer>> result = new LinkedList<>();
82+
if(nums.length == 0) return result;
83+
dfs(result, nums, new boolean[nums.length], new LinkedList<>());
84+
return result;
85+
}
86+
private void dfs(List<List<Integer>> result, int[] nums, boolean[] visited, List<Integer> temp){
87+
if(temp.size() == nums.length) result.add(new LinkedList<Integer>(temp));
88+
else{
89+
for(int i = 0; i < nums.length; i++){
90+
if(visited[i]) continue;
91+
visited[i] = true;
92+
temp.add(nums[i]);
93+
dfs(result, nums, visited, temp);
94+
temp.remove(temp.size() - 1);
95+
visited[i] = false;
96+
}
97+
}
98+
}
99+
}
100+
```

leetcode/52. N-Queens II.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,45 @@ class Solution {
105105
return true;
106106
}
107107
}
108-
```
108+
```
109+
110+
### Third time
111+
* Method 1: search + dfs
112+
* the global variable is not required, we alse can implement that using return int.
113+
```Java
114+
class Solution {
115+
public int totalNQueens(int n) {
116+
char[][] table = new char[n][n];
117+
for(int i = 0; i < n; i++)
118+
for(int j = 0; j < n; j++)
119+
table[i][j] = '.';
120+
return dfs(table, 0, n);
121+
}
122+
private int dfs(char[][] table, int row, int n){
123+
if(row == n){return 1;}
124+
else{
125+
int res = 0;
126+
for(int i = 0; i < n; i++){
127+
if(check(table, row, i, n)){
128+
table[row][i] = 'Q';
129+
res += dfs(table, row + 1, n);
130+
table[row][i] = '.';
131+
}
132+
}
133+
return res;
134+
}
135+
}
136+
private boolean check(char[][] table, int row, int col, int n){
137+
for(int i = 0; i < n; i++){
138+
if(table[row][i] == 'Q') return false;
139+
if(table[i][col] == 'Q') return false;
140+
}
141+
int r = row, c = col;
142+
while(row >= 0 && col >= 0)
143+
if(table[row--][col--] == 'Q') return false;
144+
while(r >= 0 && c < n)
145+
if(table[r--][c++] == 'Q') return false;
146+
return true;
147+
}
148+
}
149+
```

leetcode/648. Replace Words.md

100644100755
File mode changed.

leetcode/676. Implement Magic Dictionary.md

100644100755
File mode changed.

leetcode/677. Map Sum Pairs.md

100644100755
File mode changed.

leetcode/720. Longest Word in Dictionary.md

100644100755
File mode changed.

leetcode/745. Prefix and Suffix Search.md

100644100755
File mode changed.

leetcode/77. Combinations.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,4 @@ class Solution {
8888
}
8989
}
9090
}
91+
```

0 commit comments

Comments
 (0)