010 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
详见:https://leetcode.com/problems/regular-expression-matching/description/
方法一:
class Solution {
public:
bool isMatch(string s, string p) {
if(p.empty())
{
return s.empty();
}
if(p.size()==1)
{
return (s.size()==1&&(s[0]==p[0]||p[0]=='.'));
}
if(p[1]!='*')
{
if(s.empty())
{
return false;
}
return (s[0]==p[0]||p[0]=='.')&&isMatch(s.substr(1),p.substr(1));
}
while(!s.empty()&&(s[0]==p[0]||p[0]=='.'))
{
if(isMatch(s,p.substr(2)))
{
return true;
}
s=s.substr(1);
}
return isMatch(s,p.substr(2));
}
};
方法二:动态规划,dp[i][j] 表示 s[0..i] 和 p[0..j] 是否 match
class Solution {
public:
bool isMatch(string s, string p) {
int m = s.size(), n = p.size();
vector<vector<bool>> dp(m + 1, vector<bool>(n + 1, false)); dp[0][0] = true;
for (int i = 1; i <= m; i++)
{
dp[i][0] = false;
}
for (int j = 1; j <= n; j++)
{
dp[0][j] = j > 1 && '*' == p[j - 1] && dp[0][j - 2];
} for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
if (p[j - 1] != '*')
{
dp[i][j] = dp[i - 1][j - 1] && (s[i - 1] == p[j - 1] || '.' == p[j - 1]);
}
else
{
dp[i][j] = dp[i][j - 2] || (s[i - 1] == p[j - 2] || '.' == p[j - 2]) && dp[i - 1][j];
}
}
} return dp[m][n];
}
};
参考:http://www.cnblogs.com/grandyang/p/4461713.html
010 Regular Expression Matching 正则表达式匹配的更多相关文章
- [LeetCode] 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 ...
- [LeetCode]10. Regular Expression Matching正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- 10. 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 ...
- No.010 Regular Expression Matching
10. Regular Expression Matching Total Accepted: 89193 Total Submissions: 395441 Difficulty: Hard Imp ...
- LeetCode--No.010 Regular Expression Matching
10. Regular Expression Matching Total Accepted: 89193 Total Submissions: 395441 Difficulty: Hard Imp ...
- 【JAVA、C++】LeetCode 010 Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 10. Regular Expression Matching字符串.*匹配
[抄题]: Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...
随机推荐
- java多线程编程核心技术——第二章总结
第一节synchronized同步方法目录 1.1方法内的变量为线程安全的 1.2实例变量非线程安全 1.3多个对象多个锁 1.4synchronized方法与锁对象 1.5脏读 1.6synchro ...
- MYSQL主从复制配置遇到的问题
在进行配置从服务器时遇到的错误. mysql> change master to master_host='192.168.136.129',master_user='repl',master_ ...
- 关于日志类Log4j的使用
log4j 的配置 #下面定义日志输出级别是 INFO,并且配置了2个输出目的地,一个是A3,一个是console log4j.rootLogger = INFO,A3,CONSOLE #日志最低的输 ...
- JVM体系结构之六:堆Heap之2:新生代及新生代里的两个Survivor区(下一轮S0与S1交换角色,如此循环往复)、常见调优参数
一.为什么会有年轻代 我们先来屡屡,为什么需要把堆分代?不分代不能完成他所做的事情么?其实不分代完全可以,分代的唯一理由就是优化GC性能.你先想想,如果没有分代,那我们所有的对象都在一块,GC的时候我 ...
- lua 函数调用 -- 闭包详解和C调用
转自:http://www.cnblogs.com/ringofthec/archive/2010/11/05/luaClosure.html 这里, 简单的记录一下lua中闭包的知识和C闭包调用 前 ...
- sql中in和exist语句的区别?(补充了left join和right join)
in和exists(摘录自百度)in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询. 如果两个表中一个较小,一个是大表,则子查询表大的用exi ...
- centos6.5安装zookeeper教程(三)
阅读前建议先阅读: http://www.cnblogs.com/duenboa/articles/6665159.html 1. 下载安装文件zookeeper-3.4.6.tar.gz 镜像地 ...
- R:安装、导入各种包。
library和require都可以载入包,但二者存在区别. #在一个函数中,如果一个包不存在,执行到library将会停止执行,require则会继续执行.require将会根据包的存在与否返回tr ...
- Python + winpcap抓包和发包
winpcapy Python的winpcapy库可以简单地实现收发Layer2层(数据链路层,以太网)数据. winpcapy主页:https://github.com/orweis/winpcap ...
- 发现fork容易出错的一个地方
今天在看代码时发现一段有意思的代码 #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include ...