【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 ...
随机推荐
- sql事务和锁
摘自:http://www.cnblogs.com/lxconan/archive/2011/10/20/sql_transaction_n_locks_1.html 最近在项目中进行压力测试遇到了数 ...
- 关于学习session的一二
session作为一种服务器端技术,在网页中与cookie相配合可谓相得益彰,今天学习了进一步session的相关知识,在这里记录一二,以便于以后查阅 /*问题:当我们浏览网页的时候,张三和李四可以购 ...
- MVC中下拉列表绑定方法
方法一: 前端 @Html.DropDownListFor(a=>a.acate,ViewBag.CateList as IEnumerable<SelectListItem>) 后 ...
- php面向对象面试题
php面试题之四--PHP面向对象(基础部分) 四.PHP面向对象 1. 写出 php 的 public.protected.private 三种访问控制模式的区别(新浪网技术部) public:公有 ...
- apt-get 按照php7后apache 输出php源文件
需要添加php7的模块 sudo apt-get install libapache2-mod-php7.0
- js读取修改创建txt文本类型文件(.ini)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- _beginThreadex创建多线程解读【转】
_beginThreadex创建多线程解读 一.需要的头文件支持 #include <process.h> // for _beginthread() 需要的设置:Proj ...
- static 的使用
static用法小结 转自 http://blog.csdn.net/Kendiv/article/details/675941 在C语言中,static的字面意思很容易把我们导入歧途,其实它的作用有 ...
- iOS开发——源代码管理——svn 命令行下常用的几个命令
1.将文件checkout到本地目录 svn checkout path(path是服务器上的目录) 例如:svn checkout svn://192.168.1.1/pro/domai ...
- UIView的layoutSubviews和drawRect
原文: UIView的layoutSubviews和drawRect UIView的setNeedsDisplay和setNeedsLayout方法.首先两个方法都是异步执行的.setNeedsDis ...