0010.Regular Expression Matching(H)
jjc
. Regular Expression Matching(hard) 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: s could be empty and contains only lowercase letters a-z. p could be empty and contains only lowercase letters a-z, and characters like . or *. Example : Input: s = "aa" p = "a" Output: false Explanation: "a" does not match the entire string "aa". Example : Input: s = "aa" p = "a*" Output: true Explanation: '*' means zero or more of the precedeng element, 'a'. Therefore, by repeating 'a' once, it becomes "aa". Example : Input: s = "ab" p = ".*" Output: true Explanation: ".*" means "zero or more (*) of any character (.)". Example : Input: s = "aab" p = "c*a*b" Output: true Explanation: c can be repeated times, a can be repeated time. Therefore it matches "aab". Example : Input: s = "mississippi" p = "mis*is*p*." Output: false Accepted ,,,
class Solution_S4ms {
public:
bool isMatch(string s, string p) {
]();
; i < s.length() + ; ++i)
{
dp[i] = ];
memset(dp[i], , p.length()+);
}
dp[s.size()][p.size()] = true;
; i--){
; j >= ; j--)
{
bool first_match = (i < s.length() &&
(p[j] == s[i] ||
p[j] == '.'));
< p.length() && p[j+] == '*')
{
dp[i][j] = dp[i][j+] || first_match && dp[i+][j];
}
else
{
dp[i][j] = first_match && dp[i+][j+];
}
}
}
][];
}
};
class Solution_S8ms {
public:
bool isMatch(string s, string p) {
int m = s.size(), n = p.size();
vector<vector<, vector<, false));
dp[][] = true;
; i <= m; ++i) {
; j <= n; ++j) {
&& p[j - ] == '*') {
dp[i][j] = dp[i][j - ] || (i > && (s[i - ] == p[j - ] || p[j - ] == ][j]);
} else {
dp[i][j] = i > && dp[i - ][j - ] && (s[i - ] == p[j - ] || p[j - ] == '.');
}
}
}
return dp[m][n];
}
};
/* https://www.cnblogs.com/grandyang/p/4461713.html
* dp[i][j]表示s[0,i)和p[0,j)是否match
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_grandyang {
public:
bool isMatch(string s, string p) {
int m = s.size(), n = p.size();
vector<vector<, vector<, false));
dp[][] = true;
; i <= m; ++i) {
; j <= n; ++j) {
&& p[j - ] == '*') {
dp[i][j] = dp[i][j - ] || (i > && (s[i - ] == p[j - ] || p[j - ] == ][j]);
} else {
dp[i][j] = i > && dp[i - ][j - ] && (s[i - ] == p[j - ] || p[j - ] == '.');
}
}
}
return dp[m][n];
}
};
0010.Regular Expression Matching(H)的更多相关文章
- 10. Regular Expression Matching[H]正则表达式匹配
题目 Given an input string(s) and a pattern(p), implement regular expression matching with support for ...
- 刷题10. Regular Expression Matching
一.题目说明 这个题目是10. Regular Expression Matching,乍一看不是很难. 但我实现提交后,总是报错.不得已查看了答案. 二.我的做法 我的实现,最大的问题在于对.*的处 ...
- [LeetCode] Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- [LeetCode] 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...
- No.010:Regular Expression Matching
问题: Implement regular expression matching with support for '.' and '*'.'.' Matches any single charac ...
- Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【leetcode】Regular Expression Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
- 【leetcode】Regular Expression Matching (hard) ★
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 66. Regular Expression Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
随机推荐
- Oracle - 合并查询数据项
select c.channel, c.channel_name , s.show_type, s.show_type_name from T_CHANNEL_INFO c, T_SHOW_INFO ...
- 2019/12/5BJFirstDay--scrum后台+cpp项目前台环境跑起来!!!
1.配置服务器: 2.进入cd C:\java\25.beijing\06.vuejs\cpp201911221829\cpp 3.运行的命令是:npm run dev 4.先启动 5.然后再启动cp ...
- 数据库应用之--Redis+mysql实现大量数据的读写,以及高并发
一.开发背景 在项目开发过程中中遇到了以下三个需求: 1. 多个用户同时上传数据: 2. 数据库需要支持同时读写: 3. 1分钟内存储上万条数据: 根据对Mysql的测试情况,遇到以下问题: 1. 最 ...
- 笔记-读官方Git教程(2)~安装与配置
小书匠 版本管理 教程内容基本来自git官方教程,认真都了系列的文章,然后对一些重点的记录下来,做了简单的归纳并写上自己的思考. 1.安装 在基于 Debian 的发行版上,使用 apt-get安装 ...
- 洛谷P1514引水入城
题目 搜索加贪心其实并不需要用到\(DP\),搜索也是比较简单地搜索. 对于每个第一行的城市进行类似于滑雪那道题的搜索,然后记录最后一行它所覆盖的区间,易得一个一行城市只会有一个区间.然后可以在最后进 ...
- NAT双线路配置详解
- ICEM——倒角的处理
原视频下载地址: https://pan.baidu.com/s/1miHMOuk 密码: knc4
- T-MAX组--项目冲刺(第二天)
THE SECOND DAY 项目相关 作业相关 具体描述 所属班级 2019秋福大软件工程实践Z班 作业要求 团队作业第五次-项目冲刺 作业正文 T-MAX组--项目冲刺(第二天) 团队名称 T-M ...
- Vue学习手记09-mock与axios拦截的使用
01.安装 安装mock npm install mockjs 安装axios npm install axios 02.新建一个config.js文件做axios拦截 import axios fr ...
- html5表单重写
html5表单重写 一.总结 一句话总结: 表单重写用于在提交按钮上指定表单提交的各种信息,比如action <input type="submit" value=" ...