【leetcode】Regular Expression Matching

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

 1 class Solution {
 2 public:
 3     bool isMatch(const char *s, const char *p) {
 4
 5         if(s==NULL||p==NULL) return false;
 6         if(*p==‘\0‘&&*s==‘\0‘) return true;
 7         if(*p==‘\0‘&&*s!=‘\0‘) return false;
 8
 9         //如果模式串下一个字符为*
10         if(*(p+1)==‘*‘)
11         {
12             //循环比较当前字符与模式串字符是否相等
13             while((*s!=‘\0‘&&*p==‘.‘)||(*s==*p))
14             {
15                 //防止这种情况出现:"aaa", "a*a"
16                 if(isMatch(s,p+2)) return true;
17                 s++;
18             }
19             return isMatch(s,p+2);
20         }
21         else if((*s!=‘\0‘&&*p==‘.‘)||*s==*p)
22         {
23             //如果当前元素相等,则开始匹配下一个元素
24             return isMatch(s+1,p+1);
25         }
26         return false;
27     }
28 };
时间: 2024-11-06 17:02:07

【leetcode】Regular Expression Matching的相关文章

【leetcode】Regular Expression Matching (hard) ★

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 010 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 shou

LeetCode——010 Regular Expression Matching

title: LeetCode--010 Regular Expression Matching author: zzw top: false toc: true mathjax: false email: [email protected] date: 2020-02-19 22:05:32 updated: 2020-02-19 22:05:32 img: summary: categories: LeetCode tags: 字符匹配 --- Description Given an in

LeetCode (10): Regular Expression Matching [HARD]

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 ent

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][JavaScript]Regular Expression Matching

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

leetcode题解||Regular Expression Matching 问题

problem: Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the<span style="color:#ff0000;"> preceding element</span>. The matching should cover the entir