经典指数          
原因
969
浏览数
0
收藏数
 

Implement regular expression matching with support for'.'and'*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. 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", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true

     举报   纠错  
 
切换
1 个答案
class Solution { public: bool isMatch(const char *s, const char *p) { return pan(s,p,0,0); } bool pan(const char *s,const char *p,int si,int pi) { if(p[pi]=='\0') return s[si]=='\0'?true:false; if(p[pi+1]!='*') return s[si]!='\0'&&(p[pi]==s[si]||p[pi]=='.')&&pan(s,p,si+1,pi+1); for(;s[si]!='\0'&&(s[si]==p[pi]||p[pi]=='.');si++) if(pan(s,p,si,pi+2)) return true; return pan(s,p,si,pi+2); } };
 
切换
撰写答案