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.

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 == '\0')
		return *s == '\0';
	if(*(p+1) != '*'){
		if((*p==*s) || ((*p=='.') && (*s!='\0')))
                          return isMatch(s+1, p+1);
		else
			return false;
	}else{
		while(*s && (*s==*p || *p=='.'))
		{
			if(isMatch(s, p+2))
				return true;
			s++;
		}
		return isMatch(s, p+2);
	}
	return false;
    }
};
时间: 2024-10-31 23:55:16

LeetCode 10: Regular Expression Matching的相关文章

LeetCode(10) - Regular Expression Matching

非常challenge的一道题,考虑的情况特别多,当考虑到了之后,还要考虑怎么处理.题目要求是,给你两个字符串,判断正则字符串能不能表示待检字符串,一个是"aabb",另外一个是正则表达式"a*.*".该正则表达式只有两种特殊符号,一种是'.',代表的是任意字符,另一种是'*',它单独出现没有任何意义,要搭配前一个如"a*",则说明a可以连续出现零到多次,根据这个定义,上面给的例子返回的就是true.总体来说,它要注意的是以下几种情况(s是待检字

LeetCode第[10]题(Java):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 fu

No.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).Some Examples:isMatch("aa",

[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

LeetCode(10)Regular Expression Matching

题目如下: Python代码: # -*- coding:utf-8 -*- def ismatch(s,p): #先将dp[s+1][p+1]二维数组全置为False dp = [[False] * (len(s) + 1) for _ in range(len(p)+1)] dp[0][0] = True for i in range(1,len(p)): dp[i+1][0] = dp[i-1][0] and p[i] == '*' for i in range(len(p)): for

leetcode010: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

天题系列:Regular Expression Matching

烧脑神题 public class Solution { public boolean isMatch(String s, String p) { // 反正我是想不出来 http://www.cnblogs.com/springfor/p/3893593.html if(p.length()==0) return s.length()==0; if(p.length()==1) return (s.length()==1 &&(p.charAt(0)==s.charAt(0)||p.ch

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 解题报告

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