Leetcode 10. Regular Expression Matching(递归,dp)
Given an input string (s) and a pattern (p), 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).
Note:
scould be empty and contains only lowercase lettersa-z.pcould be empty and contains only lowercase lettersa-z, and characters like.or*.
Example 1:
Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".
Example 2:
Input:
s = "aa"
p = "a*"
Output: true
Explanation: '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
Example 3:
Input:
s = "ab"
p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".
Example 4:
Input:
s = "aab"
p = "c*a*b"
Output: true
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore, it matches "aab".
Example 5:
Input:
s = "mississippi"
p = "mis*is*p*."
Output: false 题解:这个题如果用暴力的想法很难想清楚,因为对于.*的处理很难,但是如果我们理解.*可匹配也可不匹配这样一个性质就很容易联想递归或者DP的思路了:
这道题分的情况的要复杂一些,先给出递归的解法:
- 若p为空,且s也为空,返回true,反之返回false
- 若p的长度为1,且s长度也为1,且相同或是p为'.'则返回true,反之返回false
- 若p的第二个字符不为*,且此时s为空则返回false,否则判断首字符是否匹配,且从各自的第二个字符开始调用递归函数匹配
- 若p的第二个字符为*,s不为空且字符匹配,调用递归函数匹配s和去掉前两个字符的p,若匹配返回true,否则s去掉首字母
- 返回调用递归函数匹配s和去掉前两个字符的p的结果
由于我对递归结束的判断实在是太恶心了。。。。于是时间和空间都很慢,不过因为是最好理解的思路,所以还是把代码扔上来:
class Solution {
public:
int in(char ch){
if(ch=='.') return ;
else if(ch=='*') return ;
else return ;
}
bool isMatch(string s, string p) {
int lens = s.length();int lenp = p.length();
if(lens== && lenp==) { return ;}
if(lens== && lenp== && s[]==p[]) {return ;}
if(lens== && lenp==) return ;
if(lens!= && lenp==) { return ;}
if(lens==){
if((in(p[])==&&in(p[])!=)||(in(p[])== &&in(p[])!=)) return ;
if(in(p[])==) return isMatch(s,p.substr(,lenp));
}
if(in(p[])==&&in(p[])!=){
if(lens==||p[]!=s[]) { return ;}
else return isMatch(s.substr(,lens),p.substr(,lenp));
}
if(in(p[])==&&in(p[])!=){
if(lens==) { return ;}
else return isMatch(s.substr(,lens),p.substr(,lenp));
}
if(in(p[])==&&in(p[])==){
if(p[]!=s[]) return isMatch(s,p.substr(,lenp));
else return max(isMatch(s.substr(,lens),p),isMatch(s,p.substr(,lenp)));
}
if(in(p[])==&&in(p[])==)
return max(isMatch(s.substr(,lens),p),isMatch(s,p.substr(,lenp)));
cout<<<<endl; return ;
}
};
后来在网上看到了大佬原来可以这么写
class Solution {
public:
bool isMatch(string s, string p) {
if (p.empty()) return s.empty();
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());
}
}
};
我们也可以用DP来解,定义一个二维的DP数组,其中dp[i][j]表示s[0,i)和p[0,j)是否match,然后有下面三种情况(下面部分摘自:https://leetcode.com/problems/regular-expression-matching/discuss/5684/9-lines-16ms-c-dp-solutions-with-explanations):
1. P[i][j] = P[i - 1][j - 1], if p[j - 1] != '*' && (s[i - 1] == p[j - 1] || p[j - 1] == '.');
2. P[i][j] = P[i][j - 2], if p[j - 1] == '*' and the pattern repeats for 0 times;
3.
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.size(), n = p.size();
vector<vector<bool>> dp(m + , vector<bool>(n + , false));
dp[][] = true;
for (int i = ; i <= m; ++i) {
for (int j = ; j <= n; ++j) {
if (j > && 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 10. Regular Expression Matching(递归,dp)的更多相关文章
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- leetcode 10. Regular Expression Matching 、44. Wildcard Matching
10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...
- [LeetCode] 10. Regular Expression Matching 正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- LeetCode (10): Regular Expression Matching [HARD]
https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...
- 蜗牛慢慢爬 LeetCode 10. Regular Expression Matching [Difficulty: Hard]
题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single charac ...
- [LeetCode] 10. Regular Expression Matching ☆☆☆☆☆
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- [LeetCode]10. Regular Expression Matching正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- [LeetCode] 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...
- Java [leetcode 10] Regular Expression Matching
问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
随机推荐
- 你知道e.g.和i.e.的区别吗?
见 i.e. 是对前面的完全举例,特指 e.g. 则是不完全举例,有可能是...也有可能是...还可能是其他. 注意,i.e. 和 e.g. 第二个点后面都常跟一个逗号.
- 用seborn的函数distplot(), jointplot(), pairplt()对数据的单变量分析绘图
1.用seaborn的distplot()函数绘制直方图.参数kde = True时会把分布曲线也画出来. 如下代码所示是绘制标准正态分布的分布图 import seaborn as sns impo ...
- kafak学习(一)
发布与订阅消息系统. 数据(消息)的发送者不会直接把消息发送给接受者,这是发布与订阅消息系统的一个特点.发布者以某种方式对消息进行分类,接受者订阅他们,以便接受特定类型的消息.发布与订阅系统一般会有一 ...
- 深入分析微博密码加密and百万级数据采集分享(登录篇)
一.前言 此文章主要是对微博微博关键词的检索结果进行采集,但是微博的采集需要登陆,所以此程序分为登陆程序和爬虫程序两部分: 微博要实现规模性数据采集自然少不了大量账号,这就需购买大量账号以及批量登陆, ...
- Python中classmethod和staticmethod的区别
学习python中经常会出现一些相近或者相似的语法模块等,需要对比分析才能加深记忆,熟练运用. staticmethod:静态方法 classmethod:类方法 在python中,静态方法和类方法都 ...
- 使用certbot 为nginx或openresty启用https
certbot 使用letsencrypt 生成免费https证书 以下内容网上整理,原文地址 https://segmentfault.com/a/1190000005797776 -------- ...
- 【Linux-驱动】驱动策略----信号量
访问共享资源的代码区块叫“临界区”,临界区需要以某种互斥机制加以保护:自旋锁.信号量等.互斥访问:一个执行单元在访问共享资源的时候,其他的执行单元被禁止访问. 信号量:在Liunx中的信号量是一种睡眠 ...
- 【监控笔记】【3.1】DML(CDC)、DDL(DDL触发器)跟踪数据更改,数据库审计
关键词:数据库审计.DDL审计.DML审计 [监控笔记][3.1]DML(CDC).DDL(DDL触发器)跟踪数据更改 [1]DML(CDC) 2008及以上 https://www.cnblogs. ...
- Mysql 5.7存储过程的学习
存储过程:对sql的封装和重用,经编译创建并保存在数据库中,通过指定存储过程的名字并给定参数(需要时)来调用执行. 优缺点: (1) 优点: 执行速度快------存储过程只在创建时进行编译,以后每次 ...
- spring boot @Transactional的一个小坑
同一个类Service下,有两个函数 method_1和 method_2,且method_1内部调用了method_2,那么希望method_2内部意外时,数据库回滚,那么一定要在method_1上 ...