[LeetCode]10.Regular Expression Matching

题目

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;
}
时间: 2024-08-29 04:57:43

[LeetCode]10.Regular Expression Matching的相关文章

leetcode 10 Regular Expression Matching(简单正则表达式匹配)

最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,python主要为了后序转型数据分析和机器学习,所以今天来做一个难度为hard 的简单正则表达式匹配. 做了很多leetcode题目,我们来总结一下套路: 首先一般是检查输入参数是否正确,然后是处理算法的特殊情况,之后就是实现逻辑,最后就是返回值. 当编程成为一种解决问题的习惯,我们就成为了一名纯粹的程序

LeetCode 10 Regular Expression Matching (正则表达式匹配)

翻译 实现支持"."和"*"的正则表达式匹配. "." 匹配支持单个字符 "*" 匹配零个或多个前面的元素 匹配应该覆盖到整个输入的字符串(而不是局部的). 该函数的原型应该是: bool isMatch(const char * s, const char * p) 示例: isMatch("aa","a") → false isMatch("aa","a

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

java正则表达式; regular expression

express|正则 概要 文本处理经常涉及的根据一个pattern的匹配.尽管java的character和assorted 的String类提供了low-level的pattern-matching支持,这种支持一般带来了复杂的代码.为了帮助你书写简单的pattern-matching代码,java提供了regular expression.在介绍给你术语和java.util.regex包之后,Jeff Friesen explores 了许多那个包的Pattern类支持的正则表达式结构.然

Regular Expression 正则表达式-1 (C#)

express|正则 起因是因为一片帖子,问到了一个问题,帖子是这样的: Originally Posted by 人就是这样我想编一个程序,但学CompSci是很久以前的事情了.想请教请教大家. 有两个txt文件,一个叫source.txt(有很多数据), 一个叫target.txt(空白的) 我想把source.txt里的一些数据提取出来(稍微修改一下),然后写到target.txt里面. 举个例子:sourse.txt里的数据:2oi)4@##( "data:001%abc"&g

共享日常收集JS正则表达式(JavaScript regular expression)_正则表达式

RegExp直接量和对象的创建 就像字符串和数字一样,程序中每个取值相同的原始类型直接量均表示相同的值,这是显而易见的.程序运行时每次遇到对象直接量(初始化表达式)诸如{}和[]的时候都会创建新对象.比如,如果在循环体中写var a = [],则每次遍历都会创建一个新的空数组.正则表达式直接量则与此不同,ECMAScript 3规范规定,一个正则表达式直接量会在执行到它时转换为一个RegExp对象,同一段代码所表示正则表达式直接量的每次运算都返回同一个对象.ECMAScript 5规范则做了相反

php 正则表达式(Regular Expression)用法

正则表达式(Regular Expression)   正则表达式系统: 1.POSIX 2.Perl   PHP中使用的regex是PCRE: NOTE:PCRE(Perl兼容正则表达式,Perl Compatible Regular Expressions)   PCRE语法: 1.定界符 必须成对出现,可以使用除0-9a-zA-Z以外的任何字符 2.原子 1.正则需要匹配的可见和不可见字符都是原子 2.一个正则表达式最少含有一个原子 3.当需要匹配诸如"("."[&qu

[LeetCode] Parse Lisp Expression 解析Lisp表达式

You are given a string expression representing a Lisp-like expression to return the integer value of. The syntax for these expressions is given as follows.  An expression is either an integer, a let-expression, an add-expression, a mult-expression, o

REGULAR EXPRESSION IN VBSCRIPT

express|vbscript|express|vbscript   REGULAR EXPRESSION IN VBSCRIPTObject RegExp is used to create and execute regular expression Ex.Code:strTarget="test testing tested attest late start"Set objRegExp= New RegExp   'create a regular expression ob