FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/code/lc10.java at master · mJackie/leetcode · GitHub
mJackie
/
leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
135
Star
405
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
code
/
lc10.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
43 lines (42 loc) · 1.82 KB
Breadcrumbs
leetcode
/
code
/
lc10.java
Copy path
File metadata and controls
43 lines (42 loc) · 1.82 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package
code
;
/*
* 10. Regular Expression Matching
* 题意:正则表达式匹配
* 难度:Hard
* 分类:String, Dynamic Programming, Backtracking
* Tips:可用递归的方法,每次判断两位字符。 也可用dp方法。
*/
public
class
lc10
{
public
static
void
main
(
String
[]
args
) {
System
.
out
.
println
(
isMatch
(
"aaa"
,
"ab*a*c*a"
));
}
public
static
boolean
isMatch
(
String
s
,
String
p
) {
// dp[i][j] = true 表示 s[0~(i-1)],p[0~(j-1)] 匹配
boolean
dp
[][] =
new
boolean
[
s
.
length
()+
1
][
p
.
length
()+
1
];
dp
[
0
][
0
] =
true
;
//初始化dp[0][i]. dp[i][0]不用初始化,因为一定为false.
for
(
int
i
=
1
;
i
<
p
.
length
()+
1
;
i
++) {
if
(
p
.
charAt
(
i
-
1
)==
'*'
)
if
(
i
>
1
&&
dp
[
0
][
i
-
2
])
dp
[
0
][
i
] =
true
;
}
for
(
int
i
=
1
;
i
<
s
.
length
()+
1
;
i
++) {
for
(
int
j
=
1
;
j
<
p
.
length
()+
1
;
j
++) {
if
(
s
.
charAt
(
i
-
1
)==
p
.
charAt
(
j
-
1
) ||
p
.
charAt
(
j
-
1
)==
'.'
)
dp
[
i
][
j
] =
dp
[
i
-
1
][
j
-
1
];
if
(
p
.
charAt
(
j
-
1
)==
'*'
) {
if
(
s
.
charAt
(
i
-
1
)!=
p
.
charAt
(
j
-
2
) &&
p
.
charAt
(
j
-
2
)!=
'.'
){
//判断s最后一位与 * 前一位是否匹配,若不匹配,则 char* 匹配空,*表示前边字符出现0次
dp
[
i
][
j
] =
dp
[
i
][
j
-
2
];
}
else
{
dp
[
i
][
j
] = (
dp
[
i
-
1
][
j
] ||
dp
[
i
-
1
][
j
-
1
] ||
dp
[
i
][
j
-
2
]);
//dp[i-1][j] * 表示前面字符出现多次
//dp[i-1][j-1] * 表示前面字符出现一次 dp[i]dp[j-1]也行
// dp[i][j-2] *表示前边字符出现0次,覆盖这些情况. s="ba" p="baa*"; s="a" p="ab*a*"
}
}
}
}
return
dp
[
s
.
length
()][
p
.
length
()];
}
}
Back
|
FazBrowse Home
|
New Git URL