【LeetCode】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
题解:
这种题是最无语的。。。我试着用遍历做,当 s 为空,p 不为空时的判定怎么也总结不出规律了。还是得用递归做
注意 ’*‘ 之前必须有字符供其匹配。s = "a", p = "*",s 和 p 不匹配。
1. 当 s 和 p 都为空时,判定 s 和 p 匹配成功
2. 当 p 元素个数大于等于 2 ,且第二个元素是‘*’时,那么有两种可能:
- ‘*’ 之前的字符匹配 0 次,即跳过这两个字符。
- s 和 p 首字符匹配 : s[0] == p[0] || p[0] == '.'。注意此时 s 需要遍历下一个字符,而 p 保持遍历元素不变,因为 此元素和 ’*‘ 可用来匹配若干次。
3. 当 p 第二个元素不是 ’*‘ 或 p 只有一个元素时,挨个去匹配。
Solution 1
class Solution {
public:
bool isMatch(string s, string p) {
if (s.empty() && p.empty())
return true;
if (p.size() > && p[] == '*') {
return isMatch(s, p.substr()) || (!s.empty() && (s[] == p[] || p[] == '.') && isMatch(s.substr(), p));
} else {
return !s.empty() && (s[] == p[] || p[] == '.') && isMatch(s.substr(), p.substr());
}
}
};
Solution 2
发现凡是字符串数组类的区间问题(匹配,区间最大最小)貌似都可以用 DP 尝试。
This problem has a typical solution using Dynamic Programming. We define the state P[i][j] to be true if s[0..i) matches p[0..j) and false otherwise. Then the state equations are:
a. P[i][j] = P[i - 1][j - 1],
if p[j - 1] != ‘*’ && (s[i - 1] == p[j - 1] || p[j - 1] == ‘.’);
b. P[i][j] = P[i][j - 2],
if p[j - 1] == ‘*’ and the pattern repeats for 0 times;
c. 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 {
public:
bool isMatch(string s, string p) {
int m = s.length(), n = p.length();
vector<vector<bool> > dp(m + , vector<bool> (n + , false));
dp[][] = true;
for (int i = ; i <= m; i++)
for (int j = ; j <= n; j++)
if (p[j - ] == '*')
dp[i][j] = dp[i][j - ] || (i > && (s[i - ] == p[j - ] || p[j - ] == '.') && dp[i - ][j]);
else dp[i][j] = i > && dp[i - ][j - ] && (s[i - ] == p[j - ] || p[j - ] == '.');
return dp[m][n];
}
};
【LeetCode】010. Regular Expression Matching的更多相关文章
- 【leetcode】10.Regular Expression Matching
题目描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
- 【LeetCode】10.Regular Expression Matching(dp)
[题意] 给两个字符串s和p,判断s是否能用p进行匹配. [题解] dp[i][j]表示s的前i个是否能被p的前j个匹配. 首先可以分成3大类情况,我们先从简单的看起: (1)s[i - 1] = p ...
- 【一天一道LeetCode】#10. Regular Expression Matching
一天一道LeetCode系列 (一)题目 Implement regular expression matching with support for '.' and '*'. '.' Matches ...
- 《LeetBook》leetcode题解(10): Regular Expression Matching——DP解决正则匹配
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【JAVA、C++】LeetCode 010 Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- LeetCode 010 Regular Expression Matching
题目描述:Regular Expression Matching Implement regular expression matching with support for '.' and '*' ...
- [Leetcode][Python][DP]Regular Expression Matching
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...
- 010 Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'.'.' Matches any single character. ...
随机推荐
- ios上ZXing库的配置流程
本文转载至 http://blog.csdn.net/louercab/article/details/26448587 步骤 首先,用Xcode创建我们的demo, 取名TestZXing(根据自己 ...
- 浅谈<持续集成、持续交付、持续部署>(一)
谈谈持续集成,持续交付,持续部署之间的区别 经常会听到持续集成,持续交付,持续部署,三者究竟是什么,有何联系和区别呢? 假如把开发工作流程分为以下几个阶段: 编码 -> 构建 -> 集 ...
- Office 365系列(-)
昨天参加上海微软TechED技术大会,看见很多传说中的大牛,听了涂曙光老师等人的讲座,激情澎湃啊,看见他们对技术以及程序员社区的投入及激情,十分敬佩.自己搞IT行业也已经10多年了,平常都很少写博客和 ...
- Notepad++ Tidy2 插件的核心配置
在已有配置的基础上加上这四行: 以免符号被转换成HTML实体了 preserve-entities: yes quote-ampersand: yes quote-marks: no quote-nb ...
- go语言之并发编程 channel(1)
单向channel: 单向通道可分为发送通道和接收通道.但是无论哪一种单向通道,都不应该出现在变量的声明中,假如初始化了这样一个变量 var uselessChan chan <- int =m ...
- python3 计算文件夹中所有py文件里面代码行数,注释行数,空行数
import os,re #代码所在位置 FILE_PATH = './' def analyze_code(codefilesource): ''' 打开一个py文件统计其中的代码行数,包括空格和注 ...
- Java中byte转换int时与0xff进行与运算的原因
http://w.baike.com/LGAdcWgJBBQxRAHUf.html 转帖 java中byte转换int时为何与0xff进行与运算 在剖析该问题前请看如下代码 public static ...
- JETSON TK1 ~ 基于eclipse下开发ROS
此文档是在PC端开发后移植到TK1,并非在TK1上安装eclipse 官方使用IDE开发的文档: http://wiki.ros.org/IDEs 一:安装eclipse 1.下载eclipse安装包 ...
- Swift URL encode
前言 在WEB前端开发,服务器后台开发,或者是客户端开发中,对URL进行编码是一件很常见的事情,但是由于各个年代的RFC文档中的内容一直在变化,一些年代久远的代码就对URL编码和解码的规则和现在的有一 ...
- c的详细学习(2)数据类型,运算符与表达式
本节用来介绍c语言中的数据类型和运算符. (1)c语言的基本符号: 任何一种基本语言都有自己的基本词汇表.c语言的基本词汇表有一下几部分: *数字10个: *英文字母:大小 ...