leetcode: Regular Expression Matching
2013-10-28 12:20
316 查看
Implementregularexpressionmatchingwithsupportfor'.'and'*'.
'.'Matchesanysinglecharacter. '*'Matcheszeroormoreoftheprecedingelement. Thematchingshouldcovertheentireinputstring(notpartial). Thefunctionprototypeshouldbe: boolisMatch(constchar*s,constchar*p) Someexamples: isMatch("aa","a")→false isMatch("aa","aa")→true isMatch("aaa","aa")→false isMatch("aa","a*")→true isMatch("aa",".*")→true isMatch("ab",".*")→true isMatch("aab","c*a*b")→true |
思路:
关键在于'*'的处理。对于类似于"a*"的情况,我们可以匹配0个'a',也可以匹配尽可能多的'a'。
classSolution{
public:
boolisMatch(constchar*s,constchar*p){
if('\0'==*p){
return'\0'==*s;
}
if('*'==*(p+1)){
while((*s!='\0')&&((*s==*p)||('.'==*p))){
if(isMatch(s,p+2)){
returntrue;
}
++s;
}
returnisMatch(s,p+2);
}
else{
if((*s!='\0')&&((*s==*p)||('.'==*p))){
returnisMatch(s+1,p+1);
}
returnfalse;
}
}
};
相关文章推荐
- LeetCode之Regular Expression Matching
- LeetCode之10_Regular Expression Matching
- Leetcode 10. Regular Expression Matching
- Leetcode 10. Regular Expression Matching
- leetcode 10 Regular Expression Matching
- LeetCode | Regular Expression Matching
- Leetcode 10 Regular Expression Matching
- Leetcode: Regular Expression Matching
- Leetcode: Regular Expression Matching 这道每次都要看答案 心塞 啊啊啊
- leetcode第十题--Regular Expression Matching
- LeetCode "Regular Expression Matching"
- leetcode 10: Regular Expression Matching
- [LeetCode]题解(python):010-Regular Expression Matching
- leetcode 10 -- Regular Expression Matching
- LeetCode 10 Regular Expression Matching
- [LeetCode] Regular Expression Matching
- LeetCode Regular Expression Matching 极品代码赏析
- LeetCode Regular Expression Matching
- [leetcode] Regular Expression Matching
- [LeetCode] Regular Expression Matching