您的位置:首页 > 其它

Regular Expression Matching

2014-08-30 22:23 260 查看
递归法,遇到*符号时,尝试从零次到多次匹配。

参考:http://leetcode.com/2011/09/regular-expression-matching.html

代码:

class Solution {
public:
bool isMatch(const char *s, const char *p) {
if(*p == '\0')  return *s == '\0';

//next character is not *
if(*(p+1) != '*')
return (*s == *p || (*p == '.' && *s))&& isMatch(s+1, p+1);
else
{
//next character is *
while(*s == *p || (*p == '.' && *s))
{
if(isMatch(s, p+2))
return true;
s++;
}
return isMatch(s, p+2);
}
}
};


动态规划法,有待补充。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: