leetcode——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
 
题意
    用p指向的正则表达式字符串来判断其是否和s指向的字符串相匹配,其中’.’代表除了’\0’之外的任意一个字符,而若某个字符后面跟了’*’则表示可以有0个或者多个这个字符。
思路
这题若没有’*’,则依次匹配,遇到*p!=*s时返回false即可,遇到’.’就算*s和*p匹配,但是有了’*’,则这题的关键点就是考虑后面带’*’的字符有0个和多个的情况。
本题用递归的方法来实现。若匹配,则isMatch返回true。
考虑p指向的字符串的字符后面带有*时要匹配0个或者多个s中的字符,则关键代码如下:
while((*p==*s) || (*p==‘.‘ && *s != ‘\0‘)){
     if(isMatch(s, p+2))
           return true;
     s++;
}

return isMatch(s, p+2);

当*s和*p是匹配时,例如s=”ab”, p=”a*b”,此时*s==’a’,*p==’a’,进入while循环
1. 进入if先匹配0个,则直接p+2,此时*p==’b’,而*s==’a’,则不成功, 退出if, 那么s++

2. s++后,此时已经默认有一个*s匹配成功,则*s现在变成’b’,再看p+2是否成功,从所给的例子中,此时*p==’b’,与*s是匹配的,会返回true;而若是其他例子中若不匹配则再s++

3. 这时可能若再能进入while循环,则默认s中已经有2个字符是和*p相匹配了,再依此类推,就可以求出0个或者多个是否匹配

若*p和*s不匹配,则跳过这个*p,试着匹配下个p指向的下个字符。

参考大神的代码后如下:

class Solution {
public:
    bool isMatch(const char *s, const char *p) {
        // 1. 遇到后面带*的字符*p,则判断*s和*p是否匹配,若匹配,则判断0个,1个和多个的情况是否有一种情况匹配
        // 2. 如没有带*,则判断*s和*p是否匹配
        // 判断s和p是否匹配:*s==*p || (*p==‘.‘ && *s!=‘\0‘),\0是字符串结尾符号
        // *p=‘.‘只能和*s!=‘\0‘的字符匹配
        if(*p==‘\0‘) return *s==‘\0‘;
        if(*(p+1) == ‘*‘){
            // 如果*p后面有*
            while((*p==*s) || (*p==‘.‘ && *s != ‘\0‘)){
                // 匹配0个, 1个和多个
                // 1. 先匹配0个,则直接p+2,若0个不成功,则s++
                // 2. s++表示匹配一个,再看p+2是否成功,否则再s++
                // 3. 这时可能匹配2个,看是否成功,若不成功则继续s++看是否和p匹配
                if(isMatch(s, p+2))
                    return true;
                s++;
            }
            // 匹配了0个或多个*p之后不成功,且*s不能和*p再匹配了,则p+2,看下个*p是否和现在这个*s匹配
            return isMatch(s, p+2);
        }else{
            // 如没有带*,则判断*s和*p是否匹配
            if((*p==*s) || (*p==‘.‘ && *s != ‘\0‘)){
                return isMatch(s+1, p+1);
            }else{ // 若不匹配则返回false
                return false;
            }
        }
    }

};
时间: 2024-10-30 10:25:08

leetcode——Regular Expression Matching的相关文章

[LeetCode] 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

[LeetCode] 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

LeetCode -- 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

LeetCode Regular Expression Matching 网上一个不错的实现(非递归)

'.' 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

[LeetCode] Regular Expression Matching

1. 只要求匹配带'.'和'*' 2. '.'可以当作任何字符来匹配 3. 只有'*'需要特殊处理,'*'可以匹配0个或者多个前面的字符. 注意处理好边界情况. class Solution { public: bool isMatch(string s, string p) { int ans = 0; int n = s.size(); int m = p.size(); vector<vector<int>> visit(n+1, vector<int>(m+1)

LeetCode: Regular Expression Matching 解题报告

Roman to IntegerGiven a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. SOLUTION 1: 思路: 从后往前遍历罗马数字,如果某个数比前一个数小,则把该数在结果中减掉:反之,则在结果中加上当前这个数: GITHUB 代码: https://github.com/yuzhangcmu/LeetCode_algorithm

LeetCode 10. Regular Expression Matching

https://leetcode.com/problems/regular-expression-matching/description/ Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover

[LeetCode][Python]Regular Expression Matching

# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement regular expression matching with support for '.' and '*'. '.' Matches any single character.'*' Matches zero or more of the preceding element. The matchin

[LEETCODE][PYTHON][DP]REGULAR EXPRESSION MATCHING

# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement regular expression matching with support for '.' and '*'. '.' Matches any single character.'*' Matches zero or more of the preceding element. The matchin