Skip to content

Commit 0ee3f19

Browse files
committed
feat: add solutions to lc/lcof problem: No.0010. Regular Expression
Matching
1 parent d186a68 commit 0ee3f19

File tree

13 files changed

+547
-143
lines changed

13 files changed

+547
-143
lines changed

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

+84-62
Original file line numberDiff line numberDiff line change
@@ -75,48 +75,44 @@ p = "mis*is*p*."
7575
```python
7676
class Solution:
7777
def isMatch(self, s: str, p: str) -> bool:
78-
m, n = len(s) + 1, len(p) + 1
79-
if n == 1:
80-
return m == 1
81-
dp = [[False for _ in range(n)] for _ in range(m)]
82-
dp[0][0], dp[0][1] = True, False
83-
for j in range(2, n):
78+
m, n = len(s), len(p)
79+
if n == 0:
80+
return m == 0
81+
dp = [[False] * (n + 1) for _ in range(m + 1)]
82+
dp[0][0] = True
83+
for j in range(2, n + 1):
8484
if p[j - 1] == '*':
8585
dp[0][j] = dp[0][j - 2]
86-
for i in range(1, m):
87-
for j in range(1, n):
86+
for i in range(1, m + 1):
87+
for j in range(1, n + 1):
8888
if s[i - 1] == p[j - 1] or p[j - 1] == '.':
8989
dp[i][j] = dp[i - 1][j - 1]
9090
elif p[j - 1] == '*':
9191
if p[j - 2] == '.' or p[j - 2] == s[i - 1]:
9292
dp[i][j] = dp[i][j - 2] or dp[i - 1][j]
9393
else:
9494
dp[i][j] = dp[i][j - 2]
95-
else:
96-
dp[i][j] = False
97-
return dp[m - 1][n - 1]
98-
95+
return dp[-1][-1]
9996
```
10097

10198
### **Java**
10299

103100
```java
104101
class Solution {
105102
public boolean isMatch(String s, String p) {
106-
int m = s.length() + 1, n = p.length() + 1;
107-
if (n == 1) {
108-
return m == 1;
103+
int m = s.length(), n = p.length();
104+
if (n == 0) {
105+
return m == 0;
109106
}
110107
boolean[][] dp = new boolean[m + 1][n + 1];
111108
dp[0][0] = true;
112-
dp[0][1] = false;
113-
for (int j = 1; j < n; ++j) {
109+
for (int j = 1; j < n + 1; ++j) {
114110
if (p.charAt(j - 1) == '*') {
115111
dp[0][j] = dp[0][j - 2];
116112
}
117113
}
118-
for (int i = 1; i < m; ++i) {
119-
for (int j = 1; j < n; ++j) {
114+
for (int i = 1; i < m + 1; ++i) {
115+
for (int j = 1; j < n + 1; ++j) {
120116
if (s.charAt(i - 1) == p.charAt(j - 1) || p.charAt(j - 1) == '.') {
121117
dp[i][j] = dp[i - 1][j - 1];
122118
} else if (p.charAt(j - 1) == '*') {
@@ -125,16 +121,82 @@ class Solution {
125121
} else {
126122
dp[i][j] = dp[i][j - 2];
127123
}
128-
} else {
129-
dp[i][j] = false;
130124
}
131125
}
132126
}
133-
return dp[m - 1][n - 1];
127+
return dp[m][n];
134128
}
135129
}
136130
```
137131

132+
### **C++**
133+
134+
```cpp
135+
class Solution {
136+
public:
137+
bool isMatch(string s, string p) {
138+
int m = s.size(), n = p.size();
139+
if (n == 0) return m == 0;
140+
vector<vector<bool>> dp(m + 1, vector<bool>(n + 1, false));
141+
dp[0][0] = true;
142+
for (int j = 1; j < n + 1; ++j) {
143+
if (p[j - 1] == '*') {
144+
dp[0][j] = dp[0][j - 2];
145+
}
146+
}
147+
for (int i = 1; i < m + 1; ++i) {
148+
for (int j = 1; j < n + 1; ++j) {
149+
if (s[i - 1] == p[j - 1] || p[j - 1] == '.') {
150+
dp[i][j] = dp[i - 1][j - 1];
151+
} else if (p[j - 1] == '*') {
152+
if (s[i - 1] == p[j - 2] || p[j - 2] == '.') {
153+
dp[i][j] = dp[i][j - 2] || dp[i - 1][j];
154+
} else {
155+
dp[i][j] = dp[i][j - 2];
156+
}
157+
}
158+
}
159+
}
160+
return dp[m][n];
161+
}
162+
};
163+
```
164+
165+
### **Go**
166+
167+
```go
168+
func isMatch(s string, p string) bool {
169+
m, n := len(s), len(p)
170+
if n == 0 {
171+
return m == 0
172+
}
173+
dp := make([][]bool, m+1)
174+
for i := 0; i < m+1; i++ {
175+
dp[i] = make([]bool, n+1)
176+
}
177+
dp[0][0] = true
178+
for j := 1; j < n+1; j++ {
179+
if p[j-1] == '*' {
180+
dp[0][j] = dp[0][j-2]
181+
}
182+
}
183+
for i := 1; i < m+1; i++ {
184+
for j := 1; j < n+1; j++ {
185+
if s[i-1] == p[j-1] || p[j-1] == '.' {
186+
dp[i][j] = dp[i-1][j-1]
187+
} else if p[j-1] == '*' {
188+
if s[i-1] == p[j-2] || p[j-2] == '.' {
189+
dp[i][j] = dp[i][j-2] || dp[i-1][j]
190+
} else {
191+
dp[i][j] = dp[i][j-2]
192+
}
193+
}
194+
}
195+
}
196+
return dp[m][n]
197+
}
198+
```
199+
138200
### **JavaScript**
139201

140202
```js
@@ -163,46 +225,6 @@ var isMatch = function (s, p) {
163225
};
164226
```
165227

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-
206228
### **...**
207229

208230
```
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
11
class Solution {
22
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;
3+
bool isMatch(string s, string p) {
4+
int m = s.size(), n = p.size();
5+
if (n == 0) return m == 0;
6+
vector<vector<bool>> dp(m + 1, vector<bool>(n + 1, false));
7+
dp[0][0] = true;
8+
for (int j = 1; j < n + 1; ++j) {
9+
if (p[j - 1] == '*') {
10+
dp[0][j] = dp[0][j - 2];
1811
}
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);
12+
}
13+
for (int i = 1; i < m + 1; ++i) {
14+
for (int j = 1; j < n + 1; ++j) {
15+
if (s[i - 1] == p[j - 1] || p[j - 1] == '.') {
16+
dp[i][j] = dp[i - 1][j - 1];
17+
} else if (p[j - 1] == '*') {
18+
if (s[i - 1] == p[j - 2] || p[j - 2] == '.') {
19+
dp[i][j] = dp[i][j - 2] || dp[i - 1][j];
20+
} else {
21+
dp[i][j] = dp[i][j - 2];
22+
}
23+
}
2424
}
2525
}
26+
return dp[m][n];
2627
}
27-
28-
bool isMatch(string s, string p) {
29-
return match(s, p, 0, 0);
30-
}
31-
};
28+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
func isMatch(s string, p string) bool {
2+
m, n := len(s), len(p)
3+
if n == 0 {
4+
return m == 0
5+
}
6+
dp := make([][]bool, m+1)
7+
for i := 0; i < m+1; i++ {
8+
dp[i] = make([]bool, n+1)
9+
}
10+
dp[0][0] = true
11+
for j := 1; j < n+1; j++ {
12+
if p[j-1] == '*' {
13+
dp[0][j] = dp[0][j-2]
14+
}
15+
}
16+
for i := 1; i < m+1; i++ {
17+
for j := 1; j < n+1; j++ {
18+
if s[i-1] == p[j-1] || p[j-1] == '.' {
19+
dp[i][j] = dp[i-1][j-1]
20+
} else if p[j-1] == '*' {
21+
if s[i-1] == p[j-2] || p[j-2] == '.' {
22+
dp[i][j] = dp[i][j-2] || dp[i-1][j]
23+
} else {
24+
dp[i][j] = dp[i][j-2]
25+
}
26+
}
27+
}
28+
}
29+
return dp[m][n]
30+
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
class Solution {
22
public boolean isMatch(String s, String p) {
3-
int m = s.length() + 1, n = p.length() + 1;
4-
if (n == 1) {
5-
return m == 1;
3+
int m = s.length(), n = p.length();
4+
if (n == 0) {
5+
return m == 0;
66
}
77
boolean[][] dp = new boolean[m + 1][n + 1];
88
dp[0][0] = true;
9-
dp[0][1] = false;
10-
for (int j = 1; j < n; ++j) {
9+
for (int j = 1; j < n + 1; ++j) {
1110
if (p.charAt(j - 1) == '*') {
1211
dp[0][j] = dp[0][j - 2];
1312
}
1413
}
15-
for (int i = 1; i < m; ++i) {
16-
for (int j = 1; j < n; ++j) {
14+
for (int i = 1; i < m + 1; ++i) {
15+
for (int j = 1; j < n + 1; ++j) {
1716
if (s.charAt(i - 1) == p.charAt(j - 1) || p.charAt(j - 1) == '.') {
1817
dp[i][j] = dp[i - 1][j - 1];
1918
} else if (p.charAt(j - 1) == '*') {
@@ -22,11 +21,9 @@ public boolean isMatch(String s, String p) {
2221
} else {
2322
dp[i][j] = dp[i][j - 2];
2423
}
25-
} else {
26-
dp[i][j] = false;
2724
}
2825
}
2926
}
30-
return dp[m - 1][n - 1];
27+
return dp[m][n];
3128
}
3229
}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
class Solution:
22
def isMatch(self, s: str, p: str) -> bool:
3-
m, n = len(s) + 1, len(p) + 1
4-
if n == 1:
5-
return m == 1
6-
dp = [[False for _ in range(n)] for _ in range(m)]
7-
dp[0][0], dp[0][1] = True, False
8-
for j in range(2, n):
3+
m, n = len(s), len(p)
4+
if n == 0:
5+
return m == 0
6+
dp = [[False] * (n + 1) for _ in range(m + 1)]
7+
dp[0][0] = True
8+
for j in range(2, n + 1):
99
if p[j - 1] == '*':
1010
dp[0][j] = dp[0][j - 2]
11-
for i in range(1, m):
12-
for j in range(1, n):
11+
for i in range(1, m + 1):
12+
for j in range(1, n + 1):
1313
if s[i - 1] == p[j - 1] or p[j - 1] == '.':
1414
dp[i][j] = dp[i - 1][j - 1]
1515
elif p[j - 1] == '*':
1616
if p[j - 2] == '.' or p[j - 2] == s[i - 1]:
1717
dp[i][j] = dp[i][j - 2] or dp[i - 1][j]
1818
else:
1919
dp[i][j] = dp[i][j - 2]
20-
else:
21-
dp[i][j] = False
22-
return dp[m - 1][n - 1]
20+
return dp[-1][-1]

lcof/面试题47. 礼物的最大价值/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class Solution {
6767
public:
6868
int maxValue(vector<vector<int>>& grid) {
6969
int m = grid.size(), n = grid[0].size();
70-
vector<vector<int> > dp(m + 1, vector<int>(n + 1, 0));
70+
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
7171
for (int i = 1; i < m + 1; ++i) {
7272
for (int j = 1; j < n + 1; ++j) {
7373
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + grid[i - 1][j - 1];

lcof/面试题47. 礼物的最大价值/Solution.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Solution {
22
public:
33
int maxValue(vector<vector<int>>& grid) {
44
int m = grid.size(), n = grid[0].size();
5-
vector<vector<int> > dp(m + 1, vector<int>(n + 1, 0));
5+
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
66
for (int i = 1; i < m + 1; ++i) {
77
for (int j = 1; j < n + 1; ++j) {
88
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + grid[i - 1][j - 1];

0 commit comments

Comments
 (0)