Skip to content

Commit f34c9a4

Browse files
committed
feat: add solutions to lcof problem: No.12
面试题12. 矩阵中的路径
1 parent 70ba383 commit f34c9a4

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

lcof/面试题12. 矩阵中的路径/README.md

+77
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,83 @@ public:
240240
};
241241
```
242242

243+
### **TypeScript**
244+
245+
```ts
246+
function exist(board: string[][], word: string): boolean {
247+
const m = board.length;
248+
const n = board[0].length;
249+
const dfs = (y: number, x: number, i: number) => {
250+
if (i === word.length) {
251+
return true;
252+
}
253+
if ((board[y] || [])[x] !== word[i]) {
254+
return false;
255+
}
256+
const temp = board[y][x];
257+
board[y][x] = '';
258+
if (
259+
dfs(y + 1, x, i + 1) ||
260+
dfs(y, x + 1, i + 1) ||
261+
dfs(y - 1, x, i + 1) ||
262+
dfs(y, x - 1, i + 1)
263+
) {
264+
return true;
265+
}
266+
board[y][x] = temp;
267+
return false;
268+
};
269+
for (let i = 0; i < m; i++) {
270+
for (let j = 0; j < n; j++) {
271+
if (dfs(i, j, 0)) {
272+
return true;
273+
}
274+
}
275+
}
276+
return false;
277+
}
278+
```
279+
280+
### **Rust**
281+
282+
```rust
283+
impl Solution {
284+
fn dfs(board: &mut Vec<Vec<char>>, chars: &Vec<char>, y: usize, x: usize, i: usize) -> bool {
285+
if board[y][x] != chars[i] {
286+
return false;
287+
}
288+
if i + 1 == chars.len() {
289+
return true;
290+
}
291+
let temp = board[y][x];
292+
board[y][x] = ' ';
293+
if y != 0 && Solution::dfs(board, chars, y - 1, x, i + 1)
294+
|| x != 0 && Solution::dfs(board, chars, y, x - 1, i + 1)
295+
|| y != board.len() - 1 && Solution::dfs(board, chars, y + 1, x, i + 1)
296+
|| x != board[0].len() - 1 && Solution::dfs(board, chars, y, x + 1, i + 1)
297+
{
298+
return true;
299+
}
300+
board[y][x] = temp;
301+
false
302+
}
303+
304+
pub fn exist(mut board: Vec<Vec<char>>, word: String) -> bool {
305+
let m = board.len();
306+
let n = board[0].len();
307+
let chars = word.chars().collect::<Vec<char>>();
308+
for i in 0..m {
309+
for j in 0..n {
310+
if Solution::dfs(&mut board, &chars, i, j, 0) {
311+
return true;
312+
}
313+
}
314+
}
315+
false
316+
}
317+
}
318+
```
319+
243320
### **...**
244321

245322
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
impl Solution {
2+
fn dfs(board: &mut Vec<Vec<char>>, chars: &Vec<char>, y: usize, x: usize, i: usize) -> bool {
3+
if board[y][x] != chars[i] {
4+
return false;
5+
}
6+
if i + 1 == chars.len() {
7+
return true;
8+
}
9+
let temp = board[y][x];
10+
board[y][x] = ' ';
11+
if y != 0 && Solution::dfs(board, chars, y - 1, x, i + 1)
12+
|| x != 0 && Solution::dfs(board, chars, y, x - 1, i + 1)
13+
|| y != board.len() - 1 && Solution::dfs(board, chars, y + 1, x, i + 1)
14+
|| x != board[0].len() - 1 && Solution::dfs(board, chars, y, x + 1, i + 1)
15+
{
16+
return true;
17+
}
18+
board[y][x] = temp;
19+
false
20+
}
21+
22+
pub fn exist(mut board: Vec<Vec<char>>, word: String) -> bool {
23+
let m = board.len();
24+
let n = board[0].len();
25+
let chars = word.chars().collect::<Vec<char>>();
26+
for i in 0..m {
27+
for j in 0..n {
28+
if Solution::dfs(&mut board, &chars, i, j, 0) {
29+
return true;
30+
}
31+
}
32+
}
33+
false
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function exist(board: string[][], word: string): boolean {
2+
const m = board.length;
3+
const n = board[0].length;
4+
const dfs = (y: number, x: number, i: number) => {
5+
if (i === word.length) {
6+
return true;
7+
}
8+
if ((board[y] || [])[x] !== word[i]) {
9+
return false;
10+
}
11+
const temp = board[y][x];
12+
board[y][x] = '';
13+
if (
14+
dfs(y + 1, x, i + 1) ||
15+
dfs(y, x + 1, i + 1) ||
16+
dfs(y - 1, x, i + 1) ||
17+
dfs(y, x - 1, i + 1)
18+
) {
19+
return true;
20+
}
21+
board[y][x] = temp;
22+
return false;
23+
};
24+
for (let i = 0; i < m; i++) {
25+
for (let j = 0; j < n; j++) {
26+
if (dfs(i, j, 0)) {
27+
return true;
28+
}
29+
}
30+
}
31+
return false;
32+
}

0 commit comments

Comments
 (0)