您的位置:首页 > 其它

LeetCode 109 Wildcard Matching

2014-10-30 12:03 381 查看
Implement wildcard pattern matching with support for '?' and '*'.

'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).

The matching should cover the entire input string (not partial).

The function prototype should be:
bool isMatch(const char *s, const char *p)

Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "*") → true
isMatch("aa", "a*") → true
isMatch("ab", "?*") → true
isMatch("aab", "c*a*b") → false分析:
通配符匹配。

?可以匹配一个字符;

*  可以匹配0个,1个或多个字符;

在s 和 p 上维持两个游标 sPos 和 pPos, 记录当前位置。

当 sPos 和 pPos 字符相等,或者 pPos 字符为 ?, 认为是匹配的,两个游标都后移;

当 pPos 字符为 *, 它可能匹配0个或多个字符,所以 pPos 后移(*可能匹配0个字符),同时记录*的位置star和s当前的位置mark,以备后面回溯回来重新匹配(*可能匹配过个字符);

如果以上都不满足,并且之前出现过*,那么我们要回溯回去,看看是不是*需要匹配多个字符。

如果以上都不行,说明不能匹配。

到最后,可能s走完了,p还没走完,这时,如果p剩余的都是*,说明可以匹配。

public class Solution {
public boolean isMatch(String s, String p) {
int sPos=0, pPos=0;
int star=-1, mark=-1;
while(sPos < s.length()){
//如果相应位置相等,或p是?,则两个下标全部后移
if( pPos<p.length() && (s.charAt(sPos)==p.charAt(pPos) || p.charAt(pPos)=='?') ){
pPos++;
sPos++;
continue;
}
//如果p是*,那么标记p上面*的位置,下标后移,同时标记s当前位置,因为*可能和空匹配
if( pPos<p.length() && (p.charAt(pPos)=='*') ){
star = pPos++;
mark = sPos;
continue;
}
//如果上面都不满足,则要p定位在*之后的位置,同时s后移,直到找到满足上面条件的
if( star != -1){
pPos = star+1;
sPos = ++mark;
continue;
}
//如果上面都不行,说明真的匹配不了
return false;
}
//如果s遍历完了,p还有剩下,就看剩下的是不是都是*
while(pPos<p.length() && p.charAt(pPos)=='*')
pPos++;
//如果都是*,则p可以定位到最后
return pPos == p.length();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息