本文主要是介绍LeetCode 题解(13):Regular Expression Matching,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
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
题解:
先给出递归做法。正在思索动态规划做法。
class Solution {
public:bool isMatch(const char *s, const char *p) {if(!*p) return *s == '\0';else if(*(p+1) != '*'){if(*s == *p || (*p == '.' && *s != '\0'))return isMatch(s+1, p+1);return false;}else {while(*s == *p || ((*p == '.' && *s != '\0'))){if(isMatch(s, p+2))return true;s++;}return isMatch(s, p+2);}}
};
这篇关于LeetCode 题解(13):Regular Expression Matching的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!