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 ...
随机推荐
- common.py OpenCv例程阅读
#!/usr/bin/env python ''' This module contais some common routines used by other samples. ''' import ...
- 网络流24题 一句话题解(updating)
搭配飞行员问题 最简单的一道题 就是一个二分图匹配 太空飞行计划 最大权闭合子图 什么叫"最大权闭合子图"呢? 就是给定一个有向图,在里面选择一个点集,使得点集中的 ...
- LINUX 文件夹打包
tar -zcvf /data/www.tar.gz data/www tar -zcvf 打包后生成的文件名全路径 要打包的目录 压缩: 压缩当前的文件夹 zip -r ./xahot.zip ./ ...
- [转]写给Git初学者的7个建议
本文转自:http://www.open-open.com/news/view/b7227e 阅读目录 第一条:花时间去学习 Git 的基本操作 第二条:从简单的 Git 工作流开始 第四条:理解分支 ...
- AJPFX关于学习java遇到的问题:对算法和数据结构不熟悉
为什么我先拿“数据结构和算法”说事捏?这玩意是写程序最最基本的东东.不管你使用 Java 还是其它的什么语言,都离不开它.而且这玩意是跨语言的,学好之后不管在哪门语言中都能用得上. 既然“数据结构和算 ...
- poj1857 To Europe! To Europe!
思路: 一维dp. 实现: #include <cstdio> #include <iostream> using namespace std; const int INF = ...
- ceph集群一键部署脚本
分布式存储ceph相信大家比较熟悉了.某项目临时要做一个40个节点的存储集群.所以写了这个脚本. 一键部署脚本如下: git clone https://github.com/luckman666/d ...
- 如何在win7、win8、win8.1上安装使用vb6.0
https://jingyan.baidu.com/article/915fc414fdf8fb51384b2062.html如何在win7.win8.win8.1上安装使用vb6.0 如何在win7 ...
- MongoDB部署、使用、监控及调优
MongoDB部署 系统环境:CentOS7 下载地址:http://mirrors.163.com/centos/7.6.1810/isos/x86_64/CentOS-7-x86_64-DVD ...
- VCS 查看代码覆盖率
代码覆盖率 代码覆盖率测试一般包括行覆盖,条件覆盖,FSM覆盖,翻转覆盖率等.在不同的代码级别有不同的覆盖率,Behavioral code包含line+condition+path(branch)+ ...