Skip to content

Commit c44142e

Browse files
ChunelFengjunfeng.fj
and
junfeng.fj
authored
增加了[lcof-12.矩阵中的路径]和[lcof-19.正则表达式匹配]的cpp版本的题解.其中,lcof-12题解双优,lcof-19主要目的是为了提供新思路 (doocs#343)
Co-authored-by: junfeng.fj <junfeng.fj@alibaba-inc.com>
1 parent 76e3417 commit c44142e

File tree

4 files changed

+170
-0
lines changed

4 files changed

+170
-0
lines changed

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

+52
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,58 @@ func bfs(board [][]byte, i, j int, isVisited [][]bool, word string, index int) b
188188
}
189189
```
190190

191+
### **C++**
192+
193+
```cpp
194+
class Solution {
195+
public:
196+
bool dfs(vector<vector<char>>& board, string& word, int cur, int x, int y) {
197+
if (board[x][y] != word[cur]) {
198+
return false;
199+
}
200+
201+
if (cur == word.size()-1) {
202+
return true;
203+
}
204+
205+
char t = board[x][y];
206+
board[x][y] = '*'; // 表示查询过了这个字段
207+
int dx[4] = {-1, 0, 1, 0};
208+
int dy[4] = {0, 1, 0, -1};
209+
for (int k = 0; k < 4; k++) {
210+
// 从上、右、下、左四个方向,开始dfs
211+
int a = x + dx[k], b = y + dy[k];
212+
if (a >= 0 && a < board.size() && b >= 0 && b < board[0].size()) {
213+
if (dfs(board, word, cur+1, a, b)) {
214+
return true;
215+
}
216+
}
217+
}
218+
219+
board[x][y] = t;
220+
return false;
221+
}
222+
223+
bool exist(vector<vector<char>>& board, string word) {
224+
int x = board.size();
225+
int y = board[0].size();
226+
if (0 == x || 0 == y) {
227+
return false;
228+
}
229+
230+
for (int i = 0; i < x; i++) {
231+
for (int j = 0; j < y; j++) {
232+
if (dfs(board, word, 0, i, j)) {
233+
return true;
234+
}
235+
}
236+
}
237+
238+
return false;
239+
}
240+
};
241+
```
242+
191243
### **...**
192244

193245
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Solution {
2+
public:
3+
bool dfs(vector<vector<char>>& board, string& word, int cur, int x, int y) {
4+
if (board[x][y] != word[cur]) {
5+
return false;
6+
}
7+
8+
if (cur == word.size()-1) {
9+
return true;
10+
}
11+
12+
char t = board[x][y];
13+
board[x][y] = '*'; // 表示查询过了这个字段
14+
int dx[4] = {-1, 0, 1, 0};
15+
int dy[4] = {0, 1, 0, -1};
16+
for (int k = 0; k < 4; k++) {
17+
// 从上、右、下、左四个方向,开始dfs
18+
int a = x + dx[k], b = y + dy[k];
19+
if (a >= 0 && a < board.size() && b >= 0 && b < board[0].size()) {
20+
if (dfs(board, word, cur+1, a, b)) {
21+
return true;
22+
}
23+
}
24+
}
25+
26+
board[x][y] = t;
27+
return false;
28+
}
29+
30+
bool exist(vector<vector<char>>& board, string word) {
31+
int x = board.size();
32+
int y = board[0].size();
33+
if (0 == x || 0 == y) {
34+
return false;
35+
}
36+
37+
for (int i = 0; i < x; i++) {
38+
for (int j = 0; j < y; j++) {
39+
if (dfs(board, word, 0, i, j)) {
40+
return true;
41+
}
42+
}
43+
}
44+
45+
return false;
46+
}
47+
};

lcof/面试题19. 正则表达式匹配/README.md

+40
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,46 @@ var isMatch = function (s, p) {
163163
};
164164
```
165165

166+
### **C++**
167+
168+
```cpp
169+
class Solution {
170+
public:
171+
bool match(string s, string p, int sl, int pl) {
172+
/* 说明:sl指的是s的len,pl指的是p的len。
173+
使用这种写法,在牛客上是能ac的。在leetcode上会显示特定的用例超时。
174+
写在这里,给大家提供一种新的思路吧。
175+
二维动态规划应该更适合做这一题的题解(参考java版本答案) */
176+
if (sl == s.size() && pl == p.size()) {
177+
return true;
178+
}
179+
180+
if (sl < s.size() && pl == p.size()) {
181+
return false;
182+
}
183+
184+
if (p[pl+1] != '*') {
185+
// 如果p的下一个不是*的情况
186+
if ((s[sl] == p[pl]) || (sl<s.size() && p[pl] == '.')) {
187+
return match(s, p, sl+1, pl+1);
188+
} else {
189+
return false;
190+
}
191+
} else {
192+
if ((s[sl] == p[pl]) || (sl<s.size() && p[pl] == '.')) {
193+
return match(s, p, sl, pl+2) || match(s, p, sl+1, pl);
194+
} else {
195+
return match(s, p, sl, pl+2);
196+
}
197+
}
198+
}
199+
200+
bool isMatch(string s, string p) {
201+
return match(s, p, 0, 0);
202+
}
203+
};
204+
```
205+
166206
### **...**
167207

168208
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
bool match(string s, string p, int sl, int pl) {
4+
if (sl == s.size() && pl == p.size()) {
5+
return true;
6+
}
7+
8+
if (sl < s.size() && pl == p.size()) {
9+
return false;
10+
}
11+
12+
if (p[pl+1] != '*') {
13+
// 如果p的下一个不是*的情况
14+
if ((s[sl] == p[pl]) || (sl<s.size() && p[pl] == '.')) {
15+
return match(s, p, sl+1, pl+1);
16+
} else {
17+
return false;
18+
}
19+
} else {
20+
if ((s[sl] == p[pl]) || (sl<s.size() && p[pl] == '.')) {
21+
return match(s, p, sl, pl+2) || match(s, p, sl+1, pl);
22+
} else {
23+
return match(s, p, sl, pl+2);
24+
}
25+
}
26+
}
27+
28+
bool isMatch(string s, string p) {
29+
return match(s, p, 0, 0);
30+
}
31+
};

0 commit comments

Comments
 (0)