【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. ...
随机推荐
- Centos 初始化服务器防火墙没有启动找不到/etc/sysconfig/iptables
个人博客:https://blog.sharedata.info/ 具体步骤:添加规则然后重启防火墙自动生成防火墙文件1.iptables -P OUTPUT ACCEPT #添加出规则2.servi ...
- python 函数的进阶
1. 动态参数 位置参数的动态参数: *args 动态接收参数的时候要注意: 动态参数必须在位置参数后面 顺序: 位置参数, 动态参数*, 默认值参数 例子: def chi(a, b, *food, ...
- Java语言实现简单FTP软件------>源码放送(十三)
Java语言实现简单FTP软件------>FTP协议分析(一) Java语言实现简单FTP软件------>FTP软件效果图预览之下载功能(二) Java语言实现简单FTP软件----- ...
- Python锁
# coding:utf-8 import threading import time def test_xc(): f = open("test.txt","a&quo ...
- C# Lambda表达式与Linq
, , , , , , }; //linq写法 var res = from i in arry select i; //lambda写法 var res = arry.Select(i => ...
- jquery链式语法
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- finally return 执行关系 异常处理 c#
Return.finally执行关系简述 除了函数出现system.exit(0)终止虚拟机,finally中的代码一定执行,return语句会等待finally的执行:如果是值传递,finally中 ...
- 【leetcode刷题笔记】Single Number
题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...
- 【九】MongoDB管理之安全性
要保证一个安全的MongoDB运行环境,DBA需要实施一些控制保证用户或应用程序仅仅访问它们需要的数据.这些措施包括但不限于: 认证机制 基于角色的访问控制 加密 审计 一.认证机制 认证是验证客户端 ...
- 操作系统原理2——OS结构
操作系统原理2——OS结构 计算机系统是由硬件系统和软件系统两部分组成, 操作系统是软件系统的一个组成部分,它是直接在硬件系统的基础上工作的,所以在研究操作系统之前,先必须对计算机系统的结构有一个 ...