Skip to content

增加了lcof-12和lcof-19的cpp版本题解 #343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions lcof/面试题12. 矩阵中的路径/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,58 @@ func bfs(board [][]byte, i, j int, isVisited [][]bool, word string, index int) b
}
```

### **C++**

```cpp
class Solution {
public:
bool dfs(vector<vector<char>>& board, string& word, int cur, int x, int y) {
if (board[x][y] != word[cur]) {
return false;
}

if (cur == word.size()-1) {
return true;
}

char t = board[x][y];
board[x][y] = '*'; // 表示查询过了这个字段
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
for (int k = 0; k < 4; k++) {
// 从上、右、下、左四个方向,开始dfs
int a = x + dx[k], b = y + dy[k];
if (a >= 0 && a < board.size() && b >= 0 && b < board[0].size()) {
if (dfs(board, word, cur+1, a, b)) {
return true;
}
}
}

board[x][y] = t;
return false;
}

bool exist(vector<vector<char>>& board, string word) {
int x = board.size();
int y = board[0].size();
if (0 == x || 0 == y) {
return false;
}

for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
if (dfs(board, word, 0, i, j)) {
return true;
}
}
}

return false;
}
};
```

### **...**

```
Expand Down
47 changes: 47 additions & 0 deletions lcof/面试题12. 矩阵中的路径/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class Solution {
public:
bool dfs(vector<vector<char>>& board, string& word, int cur, int x, int y) {
if (board[x][y] != word[cur]) {
return false;
}

if (cur == word.size()-1) {
return true;
}

char t = board[x][y];
board[x][y] = '*'; // 表示查询过了这个字段
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
for (int k = 0; k < 4; k++) {
// 从上、右、下、左四个方向,开始dfs
int a = x + dx[k], b = y + dy[k];
if (a >= 0 && a < board.size() && b >= 0 && b < board[0].size()) {
if (dfs(board, word, cur+1, a, b)) {
return true;
}
}
}

board[x][y] = t;
return false;
}

bool exist(vector<vector<char>>& board, string word) {
int x = board.size();
int y = board[0].size();
if (0 == x || 0 == y) {
return false;
}

for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
if (dfs(board, word, 0, i, j)) {
return true;
}
}
}

return false;
}
};
40 changes: 40 additions & 0 deletions lcof/面试题19. 正则表达式匹配/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,46 @@ var isMatch = function (s, p) {
};
```

### **C++**

```cpp
class Solution {
public:
bool match(string s, string p, int sl, int pl) {
/* 说明:sl指的是s的len,pl指的是p的len。
使用这种写法,在牛客上是能ac的。在leetcode上会显示特定的用例超时。
写在这里,给大家提供一种新的思路吧。
二维动态规划应该更适合做这一题的题解(参考java版本答案) */
if (sl == s.size() && pl == p.size()) {
return true;
}

if (sl < s.size() && pl == p.size()) {
return false;
}

if (p[pl+1] != '*') {
// 如果p的下一个不是*的情况
if ((s[sl] == p[pl]) || (sl<s.size() && p[pl] == '.')) {
return match(s, p, sl+1, pl+1);
} else {
return false;
}
} else {
if ((s[sl] == p[pl]) || (sl<s.size() && p[pl] == '.')) {
return match(s, p, sl, pl+2) || match(s, p, sl+1, pl);
} else {
return match(s, p, sl, pl+2);
}
}
}

bool isMatch(string s, string p) {
return match(s, p, 0, 0);
}
};
```

### **...**

```
Expand Down
31 changes: 31 additions & 0 deletions lcof/面试题19. 正则表达式匹配/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution {
public:
bool match(string s, string p, int sl, int pl) {
if (sl == s.size() && pl == p.size()) {
return true;
}

if (sl < s.size() && pl == p.size()) {
return false;
}

if (p[pl+1] != '*') {
// 如果p的下一个不是*的情况
if ((s[sl] == p[pl]) || (sl<s.size() && p[pl] == '.')) {
return match(s, p, sl+1, pl+1);
} else {
return false;
}
} else {
if ((s[sl] == p[pl]) || (sl<s.size() && p[pl] == '.')) {
return match(s, p, sl, pl+2) || match(s, p, sl+1, pl);
} else {
return match(s, p, sl, pl+2);
}
}
}

bool isMatch(string s, string p) {
return match(s, p, 0, 0);
}
};