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. TP框架---thinkphp中ajax分页

    //点击类别后要显示的内容 public function pagechuli3()//这个方法的功能是根据ajax传过来的值查询数据,再将查询出来的数据返回到ajax,返回的默认是JSON类型. { ...

  2. burp 代理的时候无法访问https网站

    今天在使用burp的时候发现不能访问https网站了,Google下面还出现这个 ERR_SSL_VERSION_OR_CIPHER_MISMATCH,于是到官网下载了一个最新的burp就可以访问了, ...

  3. 九度OJ 1260:珍珠项链 (字符串处理、DP)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:101 解决:27 题目描述: 假设有一条珍珠项链,有很多珍珠,r代表红色, b代表蓝色, w代表白色. 假设你在某一处剪开之后,你会沿着顺时 ...

  4. PAT 1063. 计算谱半径(20)

    在数学中,矩阵的“谱半径”是指其特征值的模集合的上确界.换言之,对于给定的n个复数空间的特征值{a1+b1i, ..., an+bni},它们的模为实部与虚部的平方和的开方,而“谱半径”就是最大模. ...

  5. FPGA低温不能启动分析

    FPGA低温不能启动分析 现象描写叙述:在给medium板光端机做低温试验时,分别给发送版.接收板断电又一次启动,发现有的板子在-40°能够启动,而有些板子在-20°都不能启动.须要升高温度到0°以上 ...

  6. shiro2

    mapper接口:根据用户id查询用户权限的菜单 service接口:根据用户id查询用户权限的菜单 获取用户权限范围的url 思路: 在用户认证时,认证通过,根据用户id从数据库获取用户权限范围的u ...

  7. m3u8格式转MP4

    公司直播平台使用的是七牛直播,今天有客户表示想将直播回放视频下载下来,数据妹子犯了愁,表示这个不会下载给客户,于是乎这个任务就落在了我的头上.熟练的打开视频,在 HTML 源代码播放地址为 http: ...

  8. 【leetcode刷题笔记】Set Matrix Zeroes

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 题解:因为题 ...

  9. mysql表数据压缩

    记得一次面试中,面试官问我是否知道表的压缩,这个时候我才知道mysql有个表压缩这么个功能,今天试用下看看表的压缩率怎么样. 这里分两个部分说明,第一部分:官方文档说明:第二部分:具体实例测试. [第 ...

  10. Docker 数据管理-三种数据mount方式

    可以在Container可写层存储数据,但是有三个缺点: 当Container销毁时,数据不能持久保存. Container的可写层和Container所在的主机紧耦合,不容易将数据移动到其他地方. ...