关闭

[LeetCode]10.Regular Expression Matching

标签: leetcode
1109人阅读 评论(0) 收藏 举报
分类:

题目

mplement 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

思路

这里写图片描述

代码

/*------------------------------------------------------------------------------------
*   日期:2014-04-03
*   作者:SJF0115
*   题目: 10.Regular Expression Matching
*   来源:http://oj.leetcode.com/problems/regular-expression-matching/
*   结果:AC
*   来源:LeetCode
------------------------------------------------------------------------------------*/
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

class Solution {
public:
    bool isMatch(const char *s, const char *p) {
        if(s == NULL || p == NULL || *p == '*') {
            return false;
        }
        if(*p == '\0') return *s == '\0';
        //next char is not '*': must match current character
        if(*(p+1) != '*') {
            if(*s == '\0') return false;
            if(*p != '.' && *p != *s) return false;
            return isMatch(s+1,p+1);
        }
        //next char is '*'
        else {
            int slen = strlen(s);
            if(isMatch(s,p+2)) return true;
            for(int i = 0; i < slen; ++i) {
                if(*p!='.' && *p != *(s+i)) return false;
                if(isMatch(s+i+1,p+2)) return true;
            }
            return false;
        }
    }
};

int main() {
    Solution solution;
    char* s = "abcbcd";
    char* p = "ab*bbc";
    bool result = solution.isMatch(s,p);
    cout<<result<<endl;
    return 0;
}
1
0
查看评论

Leetcode||10.Regular Expression Matching

10. Regular Expression Matching Total Accepted: 104860Total Submissions: 452500Difficulty: HardContributors: Admin Implement r...
  • Lingfu74
  • Lingfu74
  • 2016-10-27 22:48
  • 196

[LeetCode]10.Regular Expression Matching

【题目】 mplement regular expression matching with support for '.' and '*'. '.' Matches any single character. ...
  • SunnyYoona
  • SunnyYoona
  • 2014-04-03 21:16
  • 2908

[leetcode] 10.Regular Expression Matching

题目: Implement regular expression matching with support for ‘.’ and ‘*’.‘.’ Matches any single character. ‘*’ Matches zero or more of the preceding e...
  • u014673347
  • u014673347
  • 2015-08-30 21:19
  • 192

LeetCode—10.Regular Expression Matching

思路:注意一点,*表示前一个字符重复任意次(可以为0次),所以反向匹配,先判断最后一个是不是*,如果是*,再判断前面不是.,然后递归判断。 public class Solution { public boolean isMatch(String s, String p) { ...
  • corpsepiges
  • corpsepiges
  • 2015-11-02 11:23
  • 165

leetcode 10: Regular Expression Matching 分析及解答

解读: •‘.'代表一个任意字符,与其附近的字符无关 •’*‘代表o个或者多个前面的字符,该字符影响前面字符的“存在”,例如:a*={ε,a,aa,aaa,…},即当"*"表示0个前面的字符时,该字符串为空串ε。 分析:题目要求匹配整个输入字符串,即完全匹配,分如下情况...
  • u012150792
  • u012150792
  • 2016-07-22 20:56
  • 366

leetcode 10: Regular Expression Matching

Regular Expression Matching Total Accepted: 39732 Total Submissions: 192279 Implement regular expression matching with support for '.&...
  • xudli
  • xudli
  • 2015-05-01 02:22
  • 2271

★leetcode10_Regular Expression Matching[附动态规划]

一.问题描述 Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. ...
  • u014265088
  • u014265088
  • 2016-09-18 15:21
  • 926

《LeetBook》leetcode题解(10): Regular Expression Matching——DP解决正则匹配

010. Regular Expression Matching 这里面最复杂的操作是"*",这是个很可恶的操作,因为你永远不知道它多长。但是有一点,"*"不会单独出现,它一定是和前面一个字母或"."配成一对。看成一对后"X*&q...
  • hk2291976
  • hk2291976
  • 2016-04-15 21:50
  • 3060

Leetcode(python)--10.Regular Expression Matching

python解决leetcode10
  • xx_xxxxxxxxxx1120
  • xx_xxxxxxxxxx1120
  • 2017-04-28 18:33
  • 125

Leetcode: Regular Expression Matching

Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*...
  • doc_sgl
  • doc_sgl
  • 2013-10-14 23:09
  • 19706
    个人资料
    • 访问:1520703次
    • 积分:22632
    • 等级:
    • 排名:第377名
    • 原创:624篇
    • 转载:133篇
    • 译文:60篇
    • 评论:189条
    博客专栏
    文章分类
    最新评论