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 '*'. '.' ...
随机推荐
- Hibernate的持久化对象
Hibernate的持久化类 什么是持久化类 1. 持久化类:就是一个Java类(咱们编写的JavaBean),这个Java类与表建立了映射关系就可以成为是持久化类. * ...
- PAT乙级1040 有几个PAT
题目: 1040 有几个PAT (25分) 字符串 APPAPT 中包含了两个单词 PAT,其中第一个 PAT 是第 2 位(P),第 4 位(A),第 6 位(T):第二个 PAT 是第 3 位 ...
- 学习Spring-Data-Jpa(一)---JPA、Spring-Data-Jpa简介
写在前面:在国内使用比较多的ORM框架应该就是Mybatis了,但是现在SpringBoot和SpringCloud这么火爆,而Spring-Data-Jpa同样作为Spring家族的成员,它们无缝的 ...
- k8s-yaml
apiVersion: v1 #指定api版本,此值必须在kubectl apiversion中 kind: Pod #指定创建资源的角色/类型 metadata: #资源的元数据/属性 name: ...
- Problem 4 dp
$des$ 小 $Y$ 十分喜爱光学相关的问题, 一天他正在研究折射.他在平面上放置了 $n$ 个折射装置, 希望利用这些装置画出美丽的折线.折线将从某个装置出发, 并且在经过一处装置时可以转向, 若 ...
- php grpc请求go,报Yac::get(): Unserialization failed
大概说下yac是个啥东西..看鸟哥的博客 Yac 是为PHP实现的一个基于共享内存, 无锁的内容Cache Yac的两个应用场景:1.让PHP进程之间共享一些简单的数据2.高效地缓存一些页面结果 假设 ...
- Java之ClassLoader基础知识
ClassLoader基本概念 Java程序并不是一个可执行文件,而是由许多独立的类文件组成的,每一个文件对应一个Java类.这些类文件并非全部装入内存,而是根据程序需要逐渐载入.并且ClassLoa ...
- Django基础(2)-如何安装特定版本的Django项目
Django1.0版本和2.0版本的差异较大,这里jacky更常用的Django的1.9.8的版本,本小节jacky将给大家分享如何用Pycharm创建1.9.8版本的项目 (一)使用Pycharm配 ...
- CF1204E Natasha, Sasha and the Prefix Sums(组合数学)
做法一 \(O(nm)\) 考虑\(f(i,j)\)为i个+1,j个-1的贡献 \(f(i-1,j)\)考虑往序列首添加一个\(1\),则贡献\(1\times\)为序列的个数:\(C(j+i-1,i ...
- 【CSP模拟赛】仔细的检查(树的重心&树hash)
题目描述 nodgd家里种了一棵树,有一天nodgd比较无聊,就把这棵树画在了一张纸上.另一天nodgd更无聊,就又画了一张. 这时nodgd发现,两次画的顺序是不一样的,这就导致了原本的某一个节点 ...