Skip to content

Commit 6c7935e

Browse files
committedMar 7, 2020
feat: add python and java solution to lcof problem
添加《剑指 Offer》题解:面试题12. 矩阵中的路径
1 parent 31ad0da commit 6c7935e

File tree

3 files changed

+170
-0
lines changed

3 files changed

+170
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# [面试题12. 矩阵中的路径](https://leetcode-cn.com/problems/ju-zhen-zhong-de-lu-jing-lcof/)
2+
3+
## 题目描述
4+
请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一格开始,每一步可以在矩阵中向左、右、上、下移动一格。如果一条路径经过了矩阵的某一格,那么该路径不能再次进入该格子。例如,在下面的3×4的矩阵中包含一条字符串“bfce”的路径(路径中的字母用加粗标出)。
5+
6+
```
7+
[["a","b","c","e"],
8+
["s","f","c","s"],
9+
["a","d","e","e"]]
10+
```
11+
12+
但矩阵中不包含字符串“abfb”的路径,因为字符串的第一个字符 b 占据了矩阵中的第一行第二个格子之后,路径不能再次进入这个格子。
13+
14+
15+
**示例 1:**
16+
17+
```
18+
输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
19+
输出:true
20+
```
21+
22+
**示例 2:**
23+
24+
```
25+
输入:board = [["a","b"],["c","d"]], word = "abcd"
26+
输出:false
27+
```
28+
29+
**提示:**
30+
31+
- `1 <= board.length <= 200`
32+
- `1 <= board[i].length <= 200`
33+
34+
## 解法
35+
### Python3
36+
```python
37+
class Solution:
38+
def exist(self, board: List[List[str]], word: str) -> bool:
39+
if not word:
40+
return False
41+
rows, cols = len(board), len(board[0])
42+
visited = [[False for _ in range(cols)] for _ in range(rows)]
43+
for i in range(rows):
44+
for j in range(cols):
45+
if board[i][j] != word[0]:
46+
continue
47+
if self.visit(board, visited, i, j, rows, cols, word):
48+
return True
49+
return False
50+
51+
def visit(self, board, visited, i, j, rows, cols, word) -> bool:
52+
if not word:
53+
return True
54+
if i < 0 or j < 0 or i >= rows or j >= cols or visited[i][j] or board[i][j] != word[0]:
55+
return False
56+
visited[i][j] = True
57+
res = self.visit(board, visited, i - 1, j, rows, cols, word[1:]) or self.visit(board, visited, i + 1, j, rows, cols, word[1:]) or self.visit(board, visited, i, j - 1, rows, cols, word[1:]) or self.visit(board, visited, i, j + 1, rows, cols, word[1:])
58+
visited[i][j] = res
59+
return res
60+
```
61+
62+
### Java
63+
```java
64+
class Solution {
65+
public boolean exist(char[][] board, String word) {
66+
if (word == null || word.length() == 0) {
67+
return false;
68+
}
69+
int rows = board.length, cols = board[0].length;
70+
boolean[][] visited = new boolean[rows][cols];
71+
for (int i = 0; i < rows; ++i) {
72+
for (int j = 0; j < cols; ++j) {
73+
if (board[i][j] != word.charAt(0)) {
74+
continue;
75+
}
76+
if (visit(board, visited, i, j, rows, cols, word)) {
77+
return true;
78+
}
79+
}
80+
}
81+
return false;
82+
}
83+
84+
private boolean visit(char[][] board, boolean[][] visited, int i, int j, int rows, int cols, String word) {
85+
if (word.length() == 0) {
86+
return true;
87+
}
88+
if (i < 0 || j < 0 || i >= rows || j >= cols || visited[i][j] || board[i][j] != word.charAt(0)) {
89+
return false;
90+
}
91+
92+
visited[i][j] = true;
93+
String sub = word.substring(1);
94+
boolean res = visit(board, visited, i + 1, j, rows, cols, sub)
95+
|| visit(board, visited, i - 1, j, rows, cols, sub)
96+
|| visit(board, visited, i, j + 1, rows, cols, sub)
97+
|| visit(board, visited, i, j - 1, rows, cols, sub);
98+
visited[i][j] = res;
99+
return res;
100+
101+
}
102+
}
103+
```
104+
105+
### ...
106+
```
107+
108+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public boolean exist(char[][] board, String word) {
3+
if (word == null || word.length() == 0) {
4+
return false;
5+
}
6+
int rows = board.length, cols = board[0].length;
7+
boolean[][] visited = new boolean[rows][cols];
8+
for (int i = 0; i < rows; ++i) {
9+
for (int j = 0; j < cols; ++j) {
10+
if (board[i][j] != word.charAt(0)) {
11+
continue;
12+
}
13+
if (visit(board, visited, i, j, rows, cols, word)) {
14+
return true;
15+
}
16+
}
17+
}
18+
return false;
19+
}
20+
21+
private boolean visit(char[][] board, boolean[][] visited, int i, int j, int rows, int cols, String word) {
22+
if (word.length() == 0) {
23+
return true;
24+
}
25+
if (i < 0 || j < 0 || i >= rows || j >= cols || visited[i][j] || board[i][j] != word.charAt(0)) {
26+
return false;
27+
}
28+
29+
visited[i][j] = true;
30+
String sub = word.substring(1);
31+
boolean res = visit(board, visited, i + 1, j, rows, cols, sub)
32+
|| visit(board, visited, i - 1, j, rows, cols, sub)
33+
|| visit(board, visited, i, j + 1, rows, cols, sub)
34+
|| visit(board, visited, i, j - 1, rows, cols, sub);
35+
visited[i][j] = res;
36+
return res;
37+
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def exist(self, board: List[List[str]], word: str) -> bool:
3+
if not word:
4+
return False
5+
rows, cols = len(board), len(board[0])
6+
visited = [[False for _ in range(cols)] for _ in range(rows)]
7+
for i in range(rows):
8+
for j in range(cols):
9+
if board[i][j] != word[0]:
10+
continue
11+
if self.visit(board, visited, i, j, rows, cols, word):
12+
return True
13+
return False
14+
15+
def visit(self, board, visited, i, j, rows, cols, word) -> bool:
16+
if not word:
17+
return True
18+
if i < 0 or j < 0 or i >= rows or j >= cols or visited[i][j] or board[i][j] != word[0]:
19+
return False
20+
visited[i][j] = True
21+
res = self.visit(board, visited, i - 1, j, rows, cols, word[1:]) or self.visit(board, visited, i + 1, j, rows, cols, word[1:]) or self.visit(board, visited, i, j - 1, rows, cols, word[1:]) or self.visit(board, visited, i, j + 1, rows, cols, word[1:])
22+
visited[i][j] = res
23+
return res

0 commit comments

Comments
 (0)