Implement regular expression matching with support for '.' and '*'.

  1. '.' Matches any single character.
  2. '*' Matches zero or more of the preceding element.
  3.  
  4. The matching should cover the entire input string (not partial).
  5.  
  6. The function prototype should be:
  7. bool isMatch(const char *s, const char *p)
  8.  
  9. Some examples:
  10. isMatch("aa","a") false
  11. isMatch("aa","aa") true
  12. isMatch("aaa","aa") false
  13. isMatch("aa", "a*") true
  14. isMatch("aa", ".*") true
  15. isMatch("ab", ".*") true
  16. isMatch("aab", "c*a*b") true

题目是让我们自己实现正则表达式中* 和 . 的匹配功能

. 匹配任意的一个字符

*  如a*是一个整体,表示有 0个a 或 1个a 或 2个a 或..... 任意多个a

如果是 .*可以匹配 0个任意字符 或一个任意字符 或 任意多个任意字符 但这些字符必须是相同的。

思路:

开始觉的跟wildcard matching差不多,后来发现不一样,wildcard matching里面*可以随意匹配,所以当遇到后面一个*之后,前面的*就可以不用管了。

而现在这道题,*只能匹配重复的字符,所以必须考虑多个*表示的范围,所以,问题的关键就在于每个 x*都表示了多少字符。

很容易想到递归,可是写完递归后我在提交的时候各种特殊情况都通不过,每次都对特殊情况加代码,结果越加越长,加到70行仍然没AC。我默默的知道我的思路肯定是有问题了...

看大神的代码,我终于知道问题在哪了。

因为我每次都是一个字符一个字符判断的,这样遇到*之后还需要判断很多*前一个字符的情况。

但大神每次都是针对p 2个字符为一组来判断的 根据*(p+1) == '*' 来区分不同的情况,一下子就容易了很多。

还有,大神的代码凡是遇到返回值是真的情况就返回答案,不再递归

  1. class Solution {
  2. public:
  3. bool matchFirst(const char *s, const char *p){
  4. return (*p == *s || (*p == '.' && *s != '\0'));
  5. }
  6.  
  7.   bool isMatch(const char *s, const char *p) {
  8.    if (*p == '\0') return *s == '\0'; //empty
  9.   
  10.    if (*(p + ) != '*') {//without *
  11.    if(!matchFirst(s,p)) return false;
  12.   return isMatch(s + , p + );
  13.   } else { //next: with a *
  14.    if(isMatch(s, p + )) return true; //try the length of 0
  15.    while ( matchFirst(s,p) ) //try all possible lengths
  16.    if (isMatch(++s, p + ))return true;
  17.   }
  18.   }
  19. };

动态规划的方法:

用dp[i][j]表示 s[0 ~ i-1] 与 p[0 ~ j - 1] 匹配的情况, 可以匹配时true 反之为 false

那么dp[i][j]只会在以下4种情况下为真:

①dp[i-1][j-1]为真,并且s[i-1]与p[j-1]匹配

②dp[i][j-1]为真,并且p[j-1]=='*'

③dp[i-1][j]为真, 并且p[j-1]=='*' 并且 p[j-2]与s[i-1]匹配

④dp[i][j-2]为真,并且p[j-1]=='*'

边界:

dp[0][0] 都是空的肯定为真

dp[i][0] 字符串非空,匹配串为空,肯定为假

dp[0][j] 字符串空,匹配串非空,若p[j-1] == '*' 并且 dp[0][j-2]为真 的情况下 为真

代码是我参照大神的思路写的。

  1. class Solution {
  2. public:
  3. bool isMatch(const char *s, const char *p)
  4. {
  5. int m = strlen(s);
  6. int n = strlen(p);
  7. vector<vector<bool>> dp(m+, vector<bool>(n+, false));
  8. dp[][] = true;
  9. for(int i = ; i <= m; i++)
  10. {
  11. dp[i][] = false;
  12. }
  13. for(int j = ; j <= n; j++)
  14. {
  15. dp[][j] = (p[j-] == '*') && (j >= ) && dp[][j-]; //第j个字符在p中的下标是j-1,因为是从0开始的
  16. }
  17. for(int i = ; i <= m; i++)
  18. {
  19. for(int j = ; j <= n; j++)
  20. {
  21. dp[i][j] = (dp[i-][j-] && (s[i-] == p[j-] || p[j-] == '.'))
  22. || (dp[i][j-] && (p[j-] == '*'))
  23. || (dp[i-][j] && p[j-] == '*' && ((j >= ) && s[i-] == p[j-] || p[j-] == '.'))
  24. || ((j >= ) && dp[i][j-] && (p[j-] == '*'));
  25. }
  26. }
  27. return dp[m][n];
  28. }
  29. };

【leetcode】Regular Expression Matching (hard) ★的更多相关文章

  1. 【leetcode】Regular Expression Matching

    Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...

  2. Leetcode 之Regular Expression Matching(31)

    正则表达式的匹配,还是挺难的.可根据下一个字符是不是*分为两种情况处理,需要考虑多种情况. bool isMatch(const char *s, const char *p) { if (*p == ...

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

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

  4. 【LeetCode】数组--合并区间(56)

    写在前面   老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...

  5. 【LeetCode】10.Regular Expression Matching(dp)

    [题意] 给两个字符串s和p,判断s是否能用p进行匹配. [题解] dp[i][j]表示s的前i个是否能被p的前j个匹配. 首先可以分成3大类情况,我们先从简单的看起: (1)s[i - 1] = p ...

  6. 【leetcode】Number of Islands(middle)

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  7. LeetCode 10 Regular Expression Matching(字符串匹配)

    题目链接 https://leetcode.com/problems/regular-expression-matching/?tab=Description   '.' Matches any si ...

  8. 【LeetCode】数组排列问题(permutations)(附加next_permutation解析)

    描述 Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3 ...

  9. 【LeetCode】Increasing Triplet Subsequence(334)

    1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...

随机推荐

  1. 使用python标准库urllib2访问网页

    #访问不需要登录的网页import urllib2target_page_url='http://10.224.110.118/myweb/view.jsp' f = urllib2.urlopen( ...

  2. 【转载】VC维的来龙去脉

    本文转载自 火光摇曳 原文链接:VC维的来龙去脉 目录: 说说历史 Hoeffding不等式 Connection to Learning 学习可行的两个核心条件 Effective Number o ...

  3. Tomcat6.0 管理器配置

    最近忙着毕业答辩,填写材料,好多事情都给耽搁了!一个月都没有继续翻译tomcat,这回有点时间赶紧补上. 这部分,其实对开发者或者tomcat管理者来说,只要会登录页面管理器或者使用写简单的http就 ...

  4. jquery Ajax跨域调用WebServices方法

    由于公司需要开发一个手机页面,想提供给同事直接在手机上可以查询SAP资料.数据需要使用js调用webserver来获取. 因为初次使用Jquery调用Webserver,所以期间并不顺利.测试调用We ...

  5. Latex 数学符号表

  6. visual studio2010 “类视图”和“对象浏览器”图标

    “类视图”和“对象浏览器”显示一些图标,这些图标表示代码实体,例如命名空间.类.函数和变量. 下表以图文并茂的形式说明了这些图标. 图标 说明 图标 说明 namespace 方法或函数 类 运算符 ...

  7. HDOJ 4497 GCD and LCM

    组合数学 GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  8. 外网主机访问虚拟机下的web服务器(NAT端口转发)

    主机:系统win7,ip地址172.18.186.210 虚拟机:VMware Workstation 7,虚拟机下安装了Centos操作系统,ip地址是192.168.202.128,部署了LAMP ...

  9. mysql explain用法和结果的含义

    重点是第二种用法,需要深入的了解. 先看一个例子: mysql> explain select * from t_order; +----+-------------+---------+--- ...

  10. Mac os装软件时提示显示需要安装旧Java SE 6运行环境解决办法

    这个时Java版本的问题,换用合适的低版本即可,下面是官方的 https://support.apple.com/kb/DL1572?viewlocale=zh_CN&locale=en_US ...