#10 Regular Expression Match

题目链接:https://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 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

bool isMatch(char* s, char* p) {
    if(*p == '\0' && *s == '\0')
        return true;
    if(*p && *(p + 1) == '*') {       //如果p后面为*,匹配失败需要回溯
        if(*p != '.' && *s != *p || *s == '\0')   //如果当前字符不匹配,*只能取0,去掉p的首两个字符后递归匹配
            return isMatch(s, p + 2);
        if(isMatch(s, p + 2))   //取*为0次,如果能匹配,返回匹配成功;
            return true;
        return isMatch(s + 1, p);   //否则回溯,去掉s首字母(*至少匹配了一次)继续递归匹配

    }
    else if((*p == '.' && *s != '\0') || *p == *s)      //当前字符匹配,递归比较之后序列;
        return isMatch(s + 1, p + 1);
    else
        return false;
}
//注意'.'不是和所有字符都匹配,因为有可能是'\0';
时间: 2024-10-26 10:35:09

#10 Regular Expression Match的相关文章

LeetCode 10 Regular Expression Match

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

10. Regular Expression Matching && 44. Wildcard Matching

10. 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).

(待解决)LeetCode 10. Regular Expression Matching 解题报告

10. Regular Expression Matching 提交网址: https://leetcode.com/problems/regular-expression-matching/ Total Accepted: 79548 Total Submissions: 361279 Difficulty: Hard Implement regular expression matching with support for '.' and '*'. '.' Matches any sing

10. Regular Expression Matching(hard)

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

刷题10. Regular Expression Matching

一.题目说明 这个题目是10. Regular Expression Matching,乍一看不是很难. 但我实现提交后,总是报错.不得已查看了答案. 二.我的做法 我的实现,最大的问题在于对.*的处理有问题,始终无法成功. #include<iostream> using namespace std; class Solution{ public: bool isMatch(string s,string p){ bool result = true; if(s.length()<=0

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 10 regular expression matching (正则表达式匹配) (动态规划)

Leetcode 10 问题描述 Given an input string (s) and a pattern (p), 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 entir

Java [leetcode 10] Regular Expression Matching

问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. http://i.cnblogs.com/EditPosts.aspx?opt=1 The matching should cover the entire input string

【leetcode】10.Regular Expression Matching

题目描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. 解题思路: 这道题如果只考虑“.”的话其实很好完成,所以解题的关键在于处理“*”的情况.以为“*”与前一个字母有关,所以应该整体考虑ch*……的情况.ch*可以匹配0-n个s的字符串