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 '*'. '.' ...
随机推荐
- 如何轻松愉快地理解条件随机场(CRF)
https://blog.csdn.net/DCX_abc/article/details/78319246 机器学习之条件随机场(CRF): https://blog.csdn.net/wangya ...
- CentOS 7.5静默安装oracle 11g
1.安装前环境准备 1.1.配置本地yum源 #因公司内网环境,没有互联网,所以需要配置本地yum源,安装所需依赖包等. #挂载ios镜像centos7.5-1804 [root@oracle ~]# ...
- learning armbian steps(1) ----- armbian 入门知识基础学习
第一问: armbian是什么? Armbian是轻量级的Debian系统和为ARM开发板专门发行并重新编译的Ubuntu系统. 第二问: 什么场景下会用到armbian系统? 一个带有arm编译器 ...
- P3723 【[AH2017/HNOI2017]礼物】
被某大佬指出这是多项式板子!? 我们假设我们原始数列是\(a_i, c_i\), 旋转后的数列是\(a_i, b_i\),我们的增加量为x \[\sum_{i = 1}^n(a_i - b_i + x ...
- C语言实现多线程排序
#include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <string.h& ...
- 安装 sqoop
简介 Sqoop是一个用来将Hadoop(Hive.HBase)和关系型数据库中的数据相互转移的工具,可以将一个关系型数据库(例如:MySQL ,Oracle ,Postgres等)中的数据导入到Ha ...
- [树链剖分]BZOJ3589动态树
题目描述 别忘了这是一棵动态树, 每时每刻都是动态的. 小明要求你在这棵树上维护两种事件 事件0: 这棵树长出了一些果子, 即某个子树中的每个节点都会长出K个果子. 事件1: 小明希望你求出几条树枝上 ...
- Leet Code 2.两数相加
2.两数相加 题目描述 给出两个非空的链表用来表示两个非负的整数.其中,它们各自的位数是按照逆序的方式存储的,并且它们的每个节点只能存储一位数字.如果,我们将这两个数相加起来,则会返回一个新的链表来表 ...
- google镜像《转》
最新谷歌镜像列表 https://jsproxy-demo.ml 谷歌镜像F1http://go.yuxuantech.com 谷歌镜像F1,非SSLhttps://www.siwa88.net 谷歌 ...
- 谈谈你对This对象的理解?
1.this总是指向函数的直接调用者(而非间接调用者):2.如果有new关键字,this指向new出来的那个对象:3.在事件中,this指向触发这个事件的对象,特殊的是,IE中的attachEvent ...