Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
javaCoderTeam
coding-interviews
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
coding-interviews
/
code
/
offer
/
src
/
Chap3
/
ReMatch.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
42 lines (39 loc) · 1.74 KB
master
Breadcrumbs
coding-interviews
/
code
/
offer
/
src
/
Chap3
/
ReMatch.java
Copy path
Top
File metadata and controls
Code
Blame
42 lines (39 loc) · 1.74 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
package
Chap3
;
/**
* 请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。
* 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配
*/
public
class
ReMatch
{
public
boolean
match
(
char
[]
str
,
char
[]
pattern
)
{
if
(
str
==
null
||
pattern
==
null
) {
return
false
;
}
return
matchRecur
(
str
,
pattern
,
0
,
0
);
}
private
boolean
matchRecur
(
char
[]
str
,
char
[]
pattern
,
int
s
,
int
p
) {
if
(
s
==
str
.
length
&&
p
==
pattern
.
length
) {
return
true
;
}
// 模式串比文本串先到末尾,肯定没有匹配成功
if
(
p
==
pattern
.
length
&&
s
<
str
.
length
) {
return
false
;
}
// 两种情况,1、模式和文本都没有到结尾
// 2、或者文本到了结尾而文本还没有到结尾,此时肯定会调用else分支
// 第二个字符是*
if
(
p
<
pattern
.
length
-
1
&&
pattern
[
p
+
1
] ==
'*'
) {
if
((
s
<
str
.
length
&&
str
[
s
] ==
pattern
[
p
]) || (
pattern
[
p
]==
'.'
&&
s
<
str
.
length
))
return
matchRecur
(
str
,
pattern
,
s
,
p
+
2
) ||
matchRecur
(
str
,
pattern
,
s
+
1
,
p
+
2
) ||
matchRecur
(
str
,
pattern
,
s
+
1
,
p
);
else
return
matchRecur
(
str
,
pattern
,
s
,
p
+
2
);
}
// 第二个字符不是*
if
((
s
<
str
.
length
&&
str
[
s
] ==
pattern
[
p
]) || (
pattern
[
p
]==
'.'
&&
s
<
str
.
length
)) {
return
matchRecur
(
str
,
pattern
,
s
+
1
,
p
+
1
);
}
return
false
;
}
}
You can’t perform that action at this time.