Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf1bb0b4

Browse files
author
applewjg
committed
wildcard matching
Change-Id: I08140e6cfda36be0e61abb8e169dc76984cd5216
1 parentdb89887 commitf1bb0b4

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

‎WildcardMatching.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Dec 20, 2014
4+
Problem: Wildcard Matching
5+
Difficulty: Medium
6+
Source: https://oj.leetcode.com/problems/wildcard-matching/
7+
Notes:
8+
Implement wildcard pattern matching with support for '?' and '*'.
9+
'?' Matches any single character.
10+
'*' Matches any sequence of characters (including the empty sequence).
11+
The matching should cover the entire input string (not partial).
12+
The function prototype should be:
13+
bool isMatch(const char *s, const char *p)
14+
Some examples:
15+
isMatch("aa","a") ? false
16+
isMatch("aa","aa") ? true
17+
isMatch("aaa","aa") ? false
18+
isMatch("aa", "*") ? true
19+
isMatch("aa", "a*") ? true
20+
isMatch("ab", "?*") ? true
21+
isMatch("aab", "c*a*b") ? false
22+
23+
Solution: 1. if s[i] == p[j] || p[j] == '?', ++i and ++j.
24+
ii, jj, record the positon of '*' in s and p, ++j and go on.
25+
if not match, go back to star, i = ++ii;
26+
*/
27+
28+
publicclassSolution {
29+
publicbooleanisMatch(Strings,Stringp) {
30+
intii = -1,jj = -1,i =0,j =0;
31+
while (i <s.length()) {
32+
if (j <p.length() &&p.charAt(j) =='*') {
33+
while (j <p.length() &&p.charAt(j) =='*') ++j;
34+
if (j ==p.length())returntrue;
35+
ii =i;
36+
jj =j;
37+
}
38+
if (j <p.length() && (p.charAt(j) =='?' ||p.charAt(j) ==s.charAt(i))) {
39+
++i; ++j;
40+
}else {
41+
if (ii == -1)returnfalse;
42+
++ii;
43+
i =ii;
44+
j =jj;
45+
}
46+
}
47+
while (j <p.length() &&p.charAt(j) =='*') ++j;
48+
returni ==s.length() &&j ==p.length();
49+
}
50+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp