LeetCode(10) Regular Expression Matching
题目
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).
The function prototype should be:
bool isMatch(const char *s, const char *p)
Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true
分析
字符串的模式匹配问题!
核心的思路是一个动态规划
dp[i][j]表示字串 s[i…len(s)], p[j…len(p)] 是否可以匹配。
那么状态转移方程如下:
dp[i][j] = c. p[j+1] != *.
if s[i] == p[j]
dp[i][j] = dp[i+1][j+1]
else
dp[i][j] = false
c p[j+1] == ‘’ (这个情况下,要扩展 , dp[i][j] 从拓展的情况下,选择一个是真的结果)
if( s[i] == p[j] || p[j] == '.' && (*s) != '\0')
当s[i] 和 p[j] 一样的时候,例如 aba, a*b这个时候,i = 0, j = 0, 自然可以匹配a a
如果p[j] == . 因为他可以匹配任何字符,所以和相等关系有基本一样的方式。
并且每一步匹配都要递增 i 的值,如果有成立的,则返回true,否则到匹配终了,返回通配符匹配完成后的结果。
AC代码
class Solution {
public:
bool isMatch(string s, string p) {
//如果字符串为空,那么模式串为空则返回true,否则返回false
if (p.empty())
return s.empty();
//求模式串的长度
int s_len = s.length();
//求字符串的长度
int p_len = p.length();
if (p[1] == '*')
{
while ((s[0] != '\0' && p[0] == '.') || (s[0] == p[0]))
{
//字符串与模式串匹配0/1/2...个字符的情况
if (isMatch(s, p.substr(2, p_len - 2)))
return true;
s = s.substr(1, s_len - 1);
}
// 字符串与模式串不能匹配
return isMatch(s, p.substr(2, p_len - 2));
}
else
{
if ((s[0] != '\0' && p[0] == '.') || (s[0] == p[0]))
return isMatch(s.substr(1, s_len - 1), p.substr(1, p_len - 1));
return false;
}
}
};
LeetCode(10) Regular Expression Matching的更多相关文章
- LeetCode(10)Regular Expression Matching
题目如下: Python代码: # -*- coding:utf-8 -*- def ismatch(s,p): #先将dp[s+1][p+1]二维数组全置为False dp = [[False] * ...
- Leetcode(10)正则表达式匹配
Leetcode(10)正则表达式匹配 [题目表述]: 给定一个字符串 (s) 和一个字符模式 (p).实现支持 '.' 和 '*' 的正则表达式匹配. '.' 匹配任意单个字符. '*' 匹配零个或 ...
- leetcode第十题--Regular Expression Matching
Problem:Implement regular expression matching with support for '.' and '*'. '.' Matches any single c ...
- Leetcode(10)-正则表达式匹配
给定一个字符串 (s) 和一个字符模式 (p).实现支持 '.' 和 '*' 的正则表达式匹配. '.' 匹配任意单个字符. '*' 匹配零个或多个前面的元素. 匹配应该覆盖整个字符串 (s) ,而不 ...
- LeetCode(10):正则表达式匹配
Hard! 题目描述: 给定一个字符串 (s) 和一个字符模式 (p).实现支持 '.' 和 '*' 的正则表达式匹配. '.' 匹配任意单个字符. '*' 匹配零个或多个前面的元素. 匹配应该覆盖整 ...
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- 【leetcode】Regular Expression Matching (hard) ★
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- LeetCode OJ: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 ...
随机推荐
- 进程与线程(3)- python实现多线程
参考链接: https://www.jianshu.com/p/415976668b97?utm_campaign=maleskine&utm_content=note&utm_med ...
- C++构造函数详解(复制构造函数 也是 拷贝构造函数)
构造函数是干什么的 该类对象被创建时,编译系统对象分配内存空间,并自动调用该构造函数,由构造函数完成成员的初始化工作,故:构造函数的作用:初始化对象的数据成员. 构造函数的种类 1 class Com ...
- [WOJ2549]逻辑的连通性
题目描述: 数学中,假如有命题 p 一定能推出命题 q,则称 p 是 q 的充分条件,q 是 p 的必要 条件. 特别的,当 p 既是 q 的充分条件,又是 q 的必要条件时,称 p 和 q 互为 充 ...
- 洛谷 P4317 花神的数论题 || bzoj3209
https://www.lydsy.com/JudgeOnline/problem.php?id=3209 https://www.luogu.org/problemnew/show/P4317 设c ...
- Codeforces Round #323 (Div. 2)
被进爷坑了,第二天的比赛改到了12点 水 A - Asphalting Roads /************************************************ * Author ...
- 响应式布局 max-device-width 与 max-width 的区别
闲来没事,研究了一下多屏适配和响应式布局的 CSS. 第一种写法 @media screen and (max-device-width: 320px) { } @media screen and ( ...
- java.lang.NoSuchMethodError: org.hibernate.cfg.Environment.verifyProperties
我在使用jpa2+spring4+hibernate4 的时候,报错java.lang.NoSuchMethodError: org.hibernate.cfg.Environment.verifyP ...
- mysql 查询数据库参数命令
1.select @@tx_isolation; 查询数据库设置的事务隔离级别 2.desc table_name; 显示表设计 3.show create table table_name; ...
- mySQL ODBC 在windows 64位版上的驱动问题
1,问题的起源 某次编辑一个asp文件,其中访问mysql数据库的连接字符串如下: "driver={mysql odbc 3.51 driver};server=localhost;uid ...
- 46 Simple Python Exercises-Higher order functions and list comprehensions
26. Using the higher order function reduce(), write a function max_in_list() that takes a list of nu ...