【leetcode】Regular Expression Matching (hard) ★
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) ★的更多相关文章
- 【leetcode】Regular Expression Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
- Leetcode 之Regular Expression Matching(31)
正则表达式的匹配,还是挺难的.可根据下一个字符是不是*分为两种情况处理,需要考虑多种情况. bool isMatch(const char *s, const char *p) { if (*p == ...
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- 【LeetCode】数组--合并区间(56)
写在前面 老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...
- 【LeetCode】10.Regular Expression Matching(dp)
[题意] 给两个字符串s和p,判断s是否能用p进行匹配. [题解] dp[i][j]表示s的前i个是否能被p的前j个匹配. 首先可以分成3大类情况,我们先从简单的看起: (1)s[i - 1] = p ...
- 【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 ...
- LeetCode 10 Regular Expression Matching(字符串匹配)
题目链接 https://leetcode.com/problems/regular-expression-matching/?tab=Description '.' Matches any si ...
- 【LeetCode】数组排列问题(permutations)(附加next_permutation解析)
描述 Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3 ...
- 【LeetCode】Increasing Triplet Subsequence(334)
1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...
随机推荐
- 响应式Web初级入门
本文来自我的前端博客,原文地址:http://www.hacke2.cn/about-responsive/ 跨终端时代的到来 当你乘坐各种交通工具(公交.地铁.轻轨.火车)时你会发现,人们都个个低下 ...
- linux下查找包含BOM头的文件和清除BOM头命令
查找包含BOM头的文件,命令如下: grep -r -I -l $'^\xEF\xBB\xBF' ./ 这条命令会查找当前目录及子目录下所有包含BOM头的文件,并把文件名在屏幕上输出. 但 ...
- git/gitLab
gitlab安装:http://www.360doc.com/content/15/0603/14/21631240_475362133.shtml http://www.cnblogs.com/wi ...
- Javascript软键盘设计
国内大多数网站的密码在网络传输过程中都是明文的,我们目前正在做的产品也是这样的情形,这正常吗? 大家都偷懒?不重视安全?各人持有观点,有人认为明文传输并不是想象中的那么可怕,事实上正常情况下这些报文你 ...
- JS中的prototype(原文地址:http://www.cnblogs.com/yjf512/archive/2011/06/03/2071914.html)
JS中的phototype是JS中比较难理解的一个部分 本文基于下面几个知识点: 1 原型法设计模式 在.Net中可以使用clone()来实现原型法 原型法的主要思想是,现在有1个类A,我想要创建一个 ...
- ASP.NET实现验证码
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...
- Apache + Tomcat + mod_jk实现集群服务
Tomcat中的集群原理是通过组播的方式进行节点的查找并使用TCP连接进行会话的复制. 实现效果:用apache 分发请求到tomcat中的对应的项目 环境说明: 操作系统:window xp Jav ...
- (转)android图片压缩总结
原文地址:http://blog.csdn.net/cherry609195946/article/details/9264409 一.图片的存在形式 1.文件形式(即以二进制形式存在于硬盘上)2.流 ...
- U盘安装中标麒麟服务器操作系统 一 (NeoKylin 6.5)
U盘安装中标麒麟服务器操作系统(NeoKylin 6.5) 首先需要下载中标麒麟服务器操作系统的iso镜像.我这里的是NeoKylin Linux A 6.5.iso 因为超过了4GB,百度网盘不支持 ...
- IDEA 进入到项目的系统文件路径
选中项目,单击右键,在弹出的菜单中点击file path