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

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

. 匹配任意的一个字符

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

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

思路:

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

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

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

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

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

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

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

class Solution {
public:
bool matchFirst(const char *s, const char *p){
return (*p == *s || (*p == '.' && *s != '\0'));
}   bool isMatch(const char *s, const char *p) {
   if (*p == '\0') return *s == '\0'; //empty
  
   if (*(p + ) != '*') {//without *
   if(!matchFirst(s,p)) return false;
  return isMatch(s + , p + );
  } else { //next: with a *
   if(isMatch(s, p + )) return true; //try the length of 0
   while ( matchFirst(s,p) ) //try all possible lengths
   if (isMatch(++s, p + ))return true;
  }
  }
};

动态规划的方法:

用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]为真 的情况下 为真

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

class Solution {
public:
bool isMatch(const char *s, const char *p)
{
int m = strlen(s);
int n = strlen(p);
vector<vector<bool>> dp(m+, vector<bool>(n+, false));
dp[][] = true;
for(int i = ; i <= m; i++)
{
dp[i][] = false;
}
for(int j = ; j <= n; j++)
{
dp[][j] = (p[j-] == '*') && (j >= ) && dp[][j-]; //第j个字符在p中的下标是j-1,因为是从0开始的
}
for(int i = ; i <= m; i++)
{
for(int j = ; j <= n; j++)
{
dp[i][j] = (dp[i-][j-] && (s[i-] == p[j-] || p[j-] == '.'))
|| (dp[i][j-] && (p[j-] == '*'))
|| (dp[i-][j] && p[j-] == '*' && ((j >= ) && s[i-] == p[j-] || p[j-] == '.'))
|| ((j >= ) && dp[i][j-] && (p[j-] == '*'));
}
}
return dp[m][n];
}
};

【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. svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted

    svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted (2014-08-1 ...

  2. AD域服务器|两台DC无法进行复制同步

    注:本文由Colin撰写,版权所有!转载请注明原文地址,谢谢合作! 说明:前段时间公司两台域控出现了一些问题导致数据无法相互进行同步,DC之间也无法进行共享访问,网络用户无法通过计算机名映射的共享访问 ...

  3. 升级xcode7.0 第三方库不能用的解决方法(bitcode是什么鬼?)

    升级完xcode,真机运行发现报错,第三方库错误,微信SDK,高德SDK都报错,如下: ‘/Users/**/Framework/SDKs/PolymerPay/Library/mobStat/lib ...

  4. mongodb_修改器($inc/$set/$unset/$push/$pop/upsert......)

    主从复制:http://blog.csdn.net/drifterj/article/details/7833883 对于文档的更新除替换外,针对某个或多个文档只需要部分更新可使用原子的更新修改器,能 ...

  5. 兼容ie的jquery ajax文件上传

    Ajax文件上传插件很多,但兼容性各不一样,许多是对ie不兼容的,另外项目中是要求将网页内容嵌入到桌面端应用的,这样就不允许带flash的上传插件了,如:jquery uploadify...悲剧 对 ...

  6. 决策树-预测隐形眼镜类型 (ID3算法,C4.5算法,CART算法,GINI指数,剪枝,随机森林)

    1. 1.问题的引入 2.一个实例 3.基本概念 4.ID3 5.C4.5 6.CART 7.随机森林 2. 我们应该设计什么的算法,使得计算机对贷款申请人员的申请信息自动进行分类,以决定能否贷款? ...

  7. Android ListView 图片异步加载和图片内存缓存

    开发Android应用经常需要处理图片的加载问题.因为图片一般都是存放在服务器端,需要联网去加载,而这又是一个比较耗时的过程,所以Android中都是通过开启一个异步线程去加载.为了增加用户体验,给用 ...

  8. SpringDataJPA的几个使用记录

    public Page<XMGLFileTemplateDTO> findXMGLFileTemplateByConditions(XMGLFileTemplateDTO xmglFile ...

  9. mongodb 维护

    如何释放空间? 1. 先用 remove 命令删除数据 2. repair.需要停机,即便你不停机的话 mongodb 自己也会锁住直到 repair 完成.注意要有足够的磁盘空间,需要额外一倍的空间 ...

  10. 剑指Offer 包含min函数的栈

    题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数.   思路: 这个题是想得到一个时间复杂度为O(1)的min函数,所以应用一个辅助栈,压的时候,如果A栈的压入比B栈压入 ...