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 '*'. '.' ...
随机推荐
- C# 3.0 扩展方法&接口
namespace ExtensionInterfaceMethod { class Program { static void Main(string[] args) { //使用接口变量来调用扩展 ...
- Validation参数验证
在SpringBoot项目中其实已经默认引入了,如果不是sprongBoot项目则需要导入Maven <dependency> <groupId>org.hibernate.v ...
- Hive ACID和事务表支持详解
一.ACID介绍 ACID就是常见数据库事务的四大特性:Atomicity(原子性).Consistency(一致性).Isolation(隔离性).Durability(持久性). 在Hive 0. ...
- win32线程栈溢出问题 (一)
一.什么是线程栈溢出 我们都知道,每一个win32线程都会开辟一个空间,用来临时存储线程执行时所调用的一系列函数的参数.返回地址和局部变量及其他上下文信息.这个空间就是线程的栈区.栈区的容量是有限的, ...
- C 库函数 - strchr()
定义 char *strchr(const char *str, int c) 参数 str -- 要被检索的 C 字符串. c -- 在 str 中要搜索的字符 说明 该函数返回在字符串 str 中 ...
- andriod studio命名规范
标识符命名法标识符命名法最要有四种: 1 驼峰(Camel)命名法:又称小驼峰命名法,除首单词外,其余所有单词的第一个字母大写. 2 帕斯卡(pascal)命名法:又称大驼峰命名法,所有单词的第一个字 ...
- ubuntu安装mysql自动输入密码随笔记录
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password your_pass ...
- 小程序原生js获取用户权限
1.首先要有一个按钮 <view name="authorizemodal"> <view class="drawer_screen" wx: ...
- Linux/Centos下安装部署phantomjs
PhantomJS 是一个基于 WebKit 的服务器端 JavaScript API.它全面支持web而不需浏览器支持,其快速,原生支持各种Web标准: DOM 处理, CSS 选择器, JSON, ...
- Java设计模式之二工厂模式
在上一篇中我们学习了单例模式,介绍了单例模式创建的几种方法以及最优的方法.本篇则介绍设计模式中的工厂模式,主要分为简单工厂模式.工厂方法和抽象工厂模式. 简单工厂模式 简单工厂模式是属于创建型模式,又 ...