一天一道LeetCode系列

(一)题目

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

(二)解题

本题的意思是实现正则表达式的判断函数。

tips:评级为hard的题还真是难做!

考虑到aaaaaab和a*b,这种无法判断*的结束位置,需要用到动态规划来求解。

分为以下两种情况:

case 1:p[j+1] !=’*’时,无论是是s[i] == p[j]还是p[j]==’.’,状态转移方程均为dfs[i][j] = dfs[i+1][j+1];

case 2:p[j+1] == ‘‘时,这个时候就需要判断匹配的结束位置。

    while(*s == *p || *s != '\0' && *p == '.')
    {
        if(Match(s,p+2)) return true;
        s++;
    }

结束位置找到以后状态转移方程为:dfs[i][j] = dfs[i][j+2];

接下来看代码:

    class Solution {
    public:
        bool isMatch(string s, string p) {
           Match((const char *)&s[0],(const char *)&p[0]);
        }
        bool Match(const char *s,const char *p)
        {
            if(*p == '\0') return *s == '\0';
            if(*(p+1) == '*')
            {
                while(*s == *p || *s != '\0' && *p == '.')
                {
                    if(Match(s,p+2)) return true;
                    s++;
                }
                return Match(s,p+2);
            }
            else
            {
                if(*s == *p ||*s != '\0' && *p == '.')
                {
                    return Match(s+1,p+1);
                }
                return false;
            }
        }
    };

【一天一道LeetCode】#10. Regular Expression Matching的更多相关文章

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

    最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...

  2. Leetcode 10. Regular Expression Matching(递归,dp)

    10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...

  3. leetcode 10. Regular Expression Matching 、44. Wildcard Matching

    10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...

  4. [LeetCode] 10. Regular Expression Matching 正则表达式匹配

    Given an input string (s) and a pattern (p), implement regular expression matching with support for  ...

  5. LeetCode (10): Regular Expression Matching [HARD]

    https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...

  6. [LeetCode] 10. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...

  7. Java [leetcode 10] Regular Expression Matching

    问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...

  8. [leetcode]10. Regular Expression Matching正则表达式的匹配

    Given an input string (s) and a pattern (p), implement regular expression matching with support for  ...

  9. 蜗牛慢慢爬 LeetCode 10. Regular Expression Matching [Difficulty: Hard]

    题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single charac ...

  10. [LeetCode] 10. Regular Expression Matching ☆☆☆☆☆

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

随机推荐

  1. APP自动化框架LazyAndroid使用手册(2)--元素自动抓取

    作者:黄书力 概述 前面的一篇博文简要介绍了安卓自动化测试框架LazyAndroid的组成结构和基本功能,本文将详细描述此框架中元素自动抓取工具lazy-uiautomaterviewer的使用方法. ...

  2. pthon核心编程-读书笔记:知识点摘录与总结(方便理解和快速记忆)

    Python 中的列表(大小可变的数组)和字典(哈希表)就是内建于语言本身的.在核心语言中提供这些重要的构建单元,可以鼓励人们使用它们, 缩短开发时间与代码量,产生出可读性更好的代码.C不提供, c+ ...

  3. Swift类型推测在可选调用中的小提示

    我们知道Swift中协议里也有对应于Objc中的可选方法或计算属性,当然协议必须以@objc伪指令修饰否则不可以哦. 如下示例: @objc protocol Transaction{ fun com ...

  4. DoesNotExist at /admin/

    DoesNotExist at /admin/ User has no account. Request Method: GET Request URL: http://127.0.0.1:8000/ ...

  5. SQLite 创建数据库(http://www.w3cschool.cc/sqlite/sqlite-create-database.html)

    SQLite 创建数据库 SQLite 的 sqlite3 命令被用来创建新的 SQLite 数据库.您不需要任何特殊的权限即可创建一个数据. 语法 sqlite3 命令的基本语法如下: $sqlit ...

  6. 【Android 系统开发】使用 Source InSight 阅读 Android 源码

    1. 安装 Source Insight (1) Source Insight 相关资源 安装相关资源 : -- 下载地址 : http://www.sourceinsight.com/down35. ...

  7. FFmpeg源代码简单分析:avio_open2()

    ===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...

  8. Ubuntu15.10下如何使用EasyGui模块开发Python GUI

    偶然的一个机会,发现了github上的这个开源的项目,easygui for python(一个基于TKinter的模块) 感觉很是惊讶,原来python也可以这么简单的开发出一些GUI界面(究其原因 ...

  9. Dynamics CRM CRM Explorer missing from Visual Studio 2012

    CRMSDK中提供的develop toolkit工具比较适合初级开发者或者是多人团队的开发,在代码版本控制及部署上均有明显的优势. 但今天在装完这个工具后,打开vs2012可以新建一个package ...

  10. 为什么选择PostgreSQL而不是MySQL

    David Bolton是一名独立开发者,他使用PostgreSQL和MySQL都已有超过十年的时间.近日,他撰文阐述了选择PostgreSQL而不是MySQL的理由.他认为,MySQL之所以仍然如此 ...