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

+9
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

+44-2
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

+26-1
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

+42-1
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

+1
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,4 @@ class Solution {
8888
}
8989
}
9090
}
91+
```

leetcode/784. Letter Case Permutation.md

100644100755
File mode changed.

leetcode/79. Word Search.md

+33-2
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ class Solution {
162162
}
163163
}
164164
return false;
165-
}
166-
private static int[][] dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
165+
}
167166
private boolean exist(char[][] board, char[] word, int index, int row, int col, boolean[][] used){
168167
if(index == word.length) return true;
169168
else{
@@ -184,3 +183,35 @@ class Solution {
184183
}
185184
}
186185
```
186+
187+
### Third time
188+
* Method 1: Search + dfs
189+
```Java
190+
class Solution {
191+
public boolean exist(char[][] board, String word) {
192+
if(word == null || word.length() == 0) return true;
193+
int height = board.length, width = board[0].length;
194+
for(int i = 0; i < height; i++){
195+
for(int j = 0; j < width; j++){
196+
if(board[i][j] == word.charAt(0))
197+
if(dfs(board, word, i, j, 0, new boolean[height][width])) return true;
198+
}
199+
}
200+
return false;
201+
}
202+
private boolean dfs(char[][] board, String word, int i, int j, int index, boolean[][] visited){
203+
if(index == word.length() - 1) return true;
204+
visited[i][j] = true;
205+
int tempRow = 0, tempCol = 0;
206+
for(int d = 0; d < 4; d++){
207+
tempRow = i + dir[d][0];
208+
tempCol = j + dir[d][1];
209+
if(tempRow >= 0 && tempRow < visited.length && tempCol >= 0 && tempCol < visited[0].length && board[tempRow][tempCol] == word.charAt(index + 1) && !visited[tempRow][tempCol]){
210+
if(dfs(board, word, tempRow, tempCol, index + 1, visited)) return true;
211+
}
212+
}
213+
visited[i][j] = false;
214+
return false;
215+
}
216+
}
217+
```

leetcode/901. Online Stock Span.md

100644100755
File mode changed.

leetcode/907. Sum of Subarray Minimums.md

100644100755
+54-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,57 @@ Note:
3535
return (int)(sum % (Math.pow(10, 9) + 7));
3636
}
3737
}
38-
```
38+
```
39+
40+
41+
* Method 2: Monotic stack
42+
1. How many times a number will appear in the results(Not thinking about minimum)
43+
* for a element A[i], it will represents (i + 1) * (len - i + 1) times.
44+
* i + 1 means the subarrays whose end point is A[i]
45+
* (len - i + 1) means the subarrays whose start point is A[i].
46+
2. How to solve this question:
47+
* res = sum(A[i] * f[i], A[i] is the number and f[i] means the number of subarrays where A[i] is the minimum in the subarray.
48+
* f[i] = left[i] * right[i], where left[i] is the left subarray numbers where A[i] is the last element and A[i] is minimum in current array and right[i] is the minimum and first element in the subarray.
49+
* How to get left[i] and right[i]. Consider leetcode question 901.
50+
* We need to consider a corner case, two elements have the same value.
51+
52+
```Java
53+
class Solution {
54+
private static class Pair{
55+
int val;
56+
int count;
57+
public Pair(int val, int count){
58+
this.val = val;
59+
this.count = count;
60+
}
61+
}
62+
public int sumSubarrayMins(int[] A) {
63+
if(A == null || A.length == 0) return 0;
64+
Stack<Pair> left = new Stack<>();
65+
int[] leftArr = new int[A.length];
66+
Stack<Pair> right = new Stack<>();
67+
int[] rightArr = new int[A.length];
68+
for(int i = 0; i < A.length; i++){
69+
int count = 1;
70+
while(!left.isEmpty() && A[i] < left.peek().val){
71+
count += left.pop().count;
72+
}
73+
left.push(new Pair(A[i], count));
74+
leftArr[i] = count;
75+
}
76+
for(int i = A.length - 1; i >= 0; i--){
77+
int count = 1;
78+
while(!right.isEmpty() && A[i] <= right.peek().val){
79+
count += right.pop().count;
80+
}
81+
right.push(new Pair(A[i], count));
82+
rightArr[i] = count;
83+
}
84+
long res = 0;
85+
for(int i = 0; i < A.length; i++){
86+
res += A[i] * leftArr[i] * rightArr[i];
87+
}
88+
return (int)(res % (1e9 + 7));
89+
}
90+
}
91+
```

leetcode/943. Find the Shortest Superstring.md

100644100755
+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
## 943. Find the Shortest Superstring
22

3-
### Question:
3+
### Question
44
Given an array A of strings, find any smallest string that contains each string in A as a substring.
5+
56
We may assume that no string in A is substring of another string in A.
67

78
```

leetcode/996. Number of Squareful Arrays.md

100644100755
File mode changed.

0 commit comments

Comments
 (0)