一天一道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. chrome浏览器不兼容jQuery Mobile问题解决

    最近在学习jQuery Mobile.第一次运行例子的时候发现chrome总是等待,查看后台报错.错误如下所示: 最后在stackoverflow上找到一个解决方案:将以下代码放在 jquery.mo ...

  2. IP_ADD_MEMBERSHIP 失败

    /*将本机加入多播组*/ err = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,&mreq, sizeof(mreq)); if (err &l ...

  3. iOS遍历数组的同时删除元素

    我们在遍历可变数组时,最好不要做删除数组中元素的操作. 因为删除操作可能会引起数组容量的变化,导致数组越界等问题. 以前在使用for循环遍历的时候遇到过这个问题. 当时的做法是使用enumerateO ...

  4. Mac小技巧:强制退出程序的六种方法

    原帖地址: http://www.cnbeta.com/articles/175447.htm 1.使用键盘快捷键强制退出处于活跃状态的Mac程序 快捷键:Command+Option+Shift+E ...

  5. 说一说关于破解支付宝AR红包的事

    当朋友圈的你们才开始分享支付宝AR红包的消息的时候,我已经对它动了一二三四次歪脑筋了,虽然事实证明并不是那么顺利,至今我也只在电脑前识别出5个不知道在哪里的红包,其中一个还因为定位信息不符开不了. 昨 ...

  6. Android Multimedia框架总结(十七)音频开发基础知识

    请尊重分享成果,转载请注明出处,本文来自逆流的鱼yuiop,原文链接:http://blog.csdn.net/hejjunlin/article/details/53078828 近年来,唱吧,全民 ...

  7. Gazebo與ROS版本說明

    使用哪种ROS / Gazebo版本的组合 介绍 本文档提供了有关将不同版本的ROS与不同版本的Gazebo结合使用的选项的概述.建议在安装Gazebo ROS包装之前阅读它.重要!简单的分析,快速和 ...

  8. 21 FragmentTabHost +Fragment代码案例

    注意头导航标签过多会被压缩并 结构 MainActivity.java package com.qf.day21_fragmenttabhost_demo1; import com.qf.day21_ ...

  9. cassandra 如何写数据以及放置副本

    application发送数据到server application 发送请求到server 根据设置的load balance 规则从cluster中挑选一个coordinator,一般使用轮询即可 ...

  10. Eclipse中设置VM参数

    eclipse.ini -Xms256m //设置堆最小值 -Xmx1024m //设置堆最大值 Eclipse 做JVM 的分析时,需要动态设置JVM的参数来进行各种测试, 可以在下图地方进行设置 ...