- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem10.java
More file actions
Latest commit
54 lines (47 loc) · 1.82 KB
/
Copy pathProblem10.java
File metadata and controls
54 lines (47 loc) · 1.82 KB
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
44
45
46
47
48
49
50
51
52
53
54
classProblem10 {
publicstaticvoidmain(String[] args) {
Strings = "ab";
Stringp = ".*";
System.out.println(newProblem10().isMatch(s, p));
}
publicbooleanisMatch(Stringtext, Stringpattern) {
System.out.println(text + " " + pattern);
if (pattern.isEmpty()) returntext.isEmpty();
booleanfirst_match = (!text.isEmpty() &&
(pattern.charAt(0) == text.charAt(0) || pattern.charAt(0) == '.'));
if (pattern.length() >= 2 && pattern.charAt(1) == '*'){
return (isMatch(text, pattern.substring(2)) ||
(first_match && isMatch(text.substring(1), pattern)));
} else {
returnfirst_match && isMatch(text.substring(1), pattern.substring(1));
}
/*if(s.length() == 0 && p.length() == 0) {
return true;
}
if (s.length() <= 0 || p.length() <= 0) return false;
char p_pos = p.charAt(0);
char s_pos = s.charAt(0);
if (p_pos == '.') {
p_pos = s.charAt(0);
}
if (p_pos != s_pos) {
// *
if (p_pos == '*') return isMatch(s.substring(1), p.substring(1));
if (p.length() >= 2) {
// 如果下一个是 '*', p往后移2
if (p.charAt(1) == '*') {
return isMatch(s, p.substring(2));
}
}
return false;
}
if (p.length() >= 2) {
// 如果下一个是 '*', p往后移2
if (p.charAt(1) == '*') {// aa a*
if (s.charAt(1) == s_pos) return isMatch(s.substring(1), p.substring(1)) || isMatch(s.substring(1), p.substring(2));
else return isMatch(s, p.substring(2));
}
}
return isMatch(s.substring(1), p.substring(1));*/
}
}