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. Lorenzo Von Matterhorn(STL_map的应用)

    Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input standa ...

  2. intellij idea pycharm phpstorm webstorm 这些编辑器像 sublime text 一样使用 ctrl d 来操作光标多选。

    在Windows 和 Linux 快捷键是 Alt+J,在 Mac OS X 系统下快捷键是 Ctrl+G . 参考 https://blog.jetbrains.com/phpstorm/2014/ ...

  3. 栈 堆 stack heap

    点餐 做菜 Stack and Heap 堆和栈的区别 - Grandyang - 博客园 https://www.cnblogs.com/grandyang/p/4933011.html 在和计算机 ...

  4. 浅谈公平组合游戏IGC

    浅谈公平组合游戏IGC IGC简介 一个游戏满足以下条件时被叫做IGC游戏 (前面三个字是自己YY的,不必在意) 竞争性:两名玩家交替行动. 公平性:游戏进程的任意时刻,可以执行的操作和操作者本人无关 ...

  5. js实现粘贴板复制

    <a href = '#' onclick ='javascript:window.clipboardData.setData('text','${form.param}');alert('クリ ...

  6. Redis持久化——AOF(二)

    核心知识点: 1.AOF:以独立日志的方式记录写命令,重启时再执行命令.与RDB不同的是解决数据持久化的实时性,可以记录所有写操作. 2.AOF工作流程:写入命令.文件同步.文件重写.文件加载. 3. ...

  7. JQuery 双击动态编辑

             $(this).append(input);            $("input#temp").focus();            $("inp ...

  8. [原创]Scala学习:流程控制,异常处理

    1.流程控制 1)do..while def doWhile(){ var line="" do{ line = readLine() println("readline ...

  9. MySQL创建用户并授权及撤销用户权限

    这篇文章主要介绍了MySQL创建用户并授权及撤销用户权限.设置与更改用户密码.删除用户等等,需要的朋友可以参考下 MySQL中创建用户与授权的实现方法. 运行环境:widnows xp profess ...

  10. android 电池(二):android关机充电流程、充电画面显示【转】

    本文转载自:http://blog.csdn.net/xubin341719/article/details/8498580 上一篇我们讲了锂电池的充放电的流程和电池的一些特性,这一节我们重点说一下a ...