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

题解:

  这种题是最无语的。。。我试着用遍历做,当 s 为空,p 不为空时的判定怎么也总结不出规律了。还是得用递归做

  注意  ’*‘ 之前必须有字符供其匹配。s = "a", p = "*",s 和 p 不匹配。

  1. 当 s 和 p 都为空时,判定 s 和 p 匹配成功

  2. 当 p 元素个数大于等于 2 ,且第二个元素是‘*’时,那么有两种可能:

      • ‘*’ 之前的字符匹配 0 次,即跳过这两个字符。
      • s 和 p 首字符匹配 : s[0] == p[0] || p[0] == '.'。注意此时 s 需要遍历下一个字符,而 p 保持遍历元素不变,因为 此元素和 ’*‘ 可用来匹配若干次。    

  3. 当 p 第二个元素不是 ’*‘ 或 p 只有一个元素时,挨个去匹配。

Solution 1

 class Solution {
public:
bool isMatch(string s, string p) {
if (s.empty() && p.empty())
return true;
if (p.size() > && p[] == '*') {
return isMatch(s, p.substr()) || (!s.empty() && (s[] == p[] || p[] == '.') && isMatch(s.substr(), p));
} else {
return !s.empty() && (s[] == p[] || p[] == '.') && isMatch(s.substr(), p.substr());
}
}
};

Solution 2

  发现凡是字符串数组类的区间问题(匹配,区间最大最小)貌似都可以用 DP 尝试。

 This problem has a typical solution using Dynamic Programming. We define the state P[i][j] to be true if s[0..i) matches p[0..j) and false otherwise. Then the state equations are: 
a. P[i][j] = P[i - 1][j - 1],                   

  if p[j - 1] != ‘*’ && (s[i - 1] == p[j - 1] || p[j - 1] == ‘.’); 
b. P[i][j] = P[i][j - 2],                   

  if p[j - 1] == ‘*’ and the pattern repeats for 0 times; 
c. P[i][j] = P[i - 1][j] && (s[i - 1] == p[j - 2] || p[j - 2] == ‘.’),  

  if p[j - 1] == ‘*’ and the pattern repeats for at least 1 times.

 class Solution {
public:
bool isMatch(string s, string p) {
int m = s.length(), n = p.length();
vector<vector<bool> > dp(m + , vector<bool> (n + , false));
dp[][] = true;
for (int i = ; i <= m; i++)
for (int j = ; j <= n; j++)
if (p[j - ] == '*')
dp[i][j] = dp[i][j - ] || (i > && (s[i - ] == p[j - ] || p[j - ] == '.') && dp[i - ][j]);
else dp[i][j] = i > && dp[i - ][j - ] && (s[i - ] == p[j - ] || p[j - ] == '.');
return dp[m][n];
}
};

【LeetCode】010. Regular Expression Matching的更多相关文章

  1. 【leetcode】10.Regular Expression Matching

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

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

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

  3. 【一天一道LeetCode】#10. Regular Expression Matching

    一天一道LeetCode系列 (一)题目 Implement regular expression matching with support for '.' and '*'. '.' Matches ...

  4. 《LeetBook》leetcode题解(10): Regular Expression Matching——DP解决正则匹配

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  5. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  6. 【JAVA、C++】LeetCode 010 Regular Expression Matching

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

  7. LeetCode 010 Regular Expression Matching

    题目描述:Regular Expression Matching Implement regular expression matching with support for '.' and '*' ...

  8. [Leetcode][Python][DP]Regular Expression Matching

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...

  9. 010 Regular Expression Matching 正则表达式匹配

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

随机推荐

  1. java服务安装(一):使用java service wrapper及maven打zip包

    目录(?)[+] 1概述 1_1为什么要用服务形式运行 1_2如何让java程序以服务形式运行 1_3打包需求 2程序示例 3maven打zip包 3_1maven-assembly-plugin介绍 ...

  2. Inside a super fast CSS engine: Quantum CSS (aka Stylo)

    https://hacks.mozilla.org/2017/08/inside-a-super-fast-css-engine-quantum-css-aka-stylo/?utm_source=d ...

  3. Python菜鸟之路:Python基础-模块

    什么是模块? 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护.为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,分组的规则就是把实现了某个 ...

  4. Bootstrap导航栏头部错位问题

    代码: <section class="header"> <div class="container"> <div class=& ...

  5. 还在用 kill -9 停机?这才是最优雅的姿势(转)

    _ 最近瞥了一眼项目的重启脚本,发现运维一直在使用 kill-9<pid> 的方式重启 springboot embedded tomcat,其实大家几乎一致认为:kill-9<pi ...

  6. linux c编程:线程互斥二 线程死锁

    死锁就是不同的程序在运行时因为某种原因发生了阻塞,进而导致程序不能正常运行.阻塞程序的原因通常都是由于程序没有正确使用临界资源. 我们举个日常生活中的例子来比喻死锁.我们把马路上行驶的汽车比作运行着的 ...

  7. VOFM 例程

    SAP ERP 实施中,经常会用到例程开发(TCODE:VOFM).这个开发目前我用到的是影响SD和MM的定价过程.创建例程需要ACCESS KEY,这个可以通过申请得到,创建后例程会被包含在一个RE ...

  8. linux基础part2

    linux基础 一.linux基础命令 1.pwd:用来显示当前目录位置 2.cd:用来切换目录位置.(eg:cd...cd../...cd-.cd~) 3.ls:用来查看目录或文件信息(eg:ls ...

  9. HTML相对路径与绝对路径

    在网页制作的过程中,少不了跟路径打交道,比如,包含一个文件,插入一个图片等,与路径都有关系,如果使用了错误的文件路径,就会导致引用失效(无法浏览链接文件,或无法显示插入的图片等).初学者可能会感到困惑 ...

  10. 期刊搜索问题——SCI、EI、IEEE和中文期刊

    1.SCI.EI收录是什么意思? SCI和EI都是收录,并不是实体的期刊出版社,相当于具有高品质期刊出版社的合体(或统充),隔一段时间(几年或者几个月)SCI会进行评估,哪些出版社不具有被SCI收录的 ...