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 ...
随机推荐
- c++ 头文件路径选择
单文件引用头文件./ 当前目录 ../ 父级目录 / 根目录 多文件引用头文件多文件引用头文件 定义单独放在cpp文件里面 ,声明放在().h)里面
- ssh公私密钥的生成
ssh密钥的生成 root账号密钥的生成: 这里我们切换到root账号下,执行ssh-keygen命令: ssh-keygen -t dsa 然后一路回车即可 """ [ ...
- noip初赛
运算符 联结词名称 异或 ⊕ 非¬(-) 否定 与∧(·) 对应集合∩交集 对应按位与符号& 合取 或∨(+) 对应集合∪并集 对应按位或符号| 析取 条件→ ...
- JVM内存配置参数-XMX,-XMS,-XMN的例子
转载:http://www.nowcoder.com/questionTerminal/093bfa948d144ce3b0a68b938ae8b4ec 对于JVM内存配置参数: -Xmx10240m ...
- android开发学习 ------- 【转】EventBus的学习理解
EventBus是一个Android端优化的publish/subscribe消息总线,简化了应用程序内各组件间.组件与后台线程间的通信. 比如请求网络,等网络返回时通过Handler或Broadca ...
- Java_静态变量
class c1c { private static int num = 0; private static double pi = 3.14; private double radius; priv ...
- ES6学习笔记(6)----函数的扩展
参考书<ECMAScript 6入门>http://es6.ruanyifeng.com/ 函数的扩展 函数的默认值 : ES6可以为函数指定默认值 (1)指定默认值的两种方式 a.函数参 ...
- 初试springWebMVC
最近在尝试配置SpringMVC,发现各种坑. 首先遇到了这个问题. 'component-scan' and its parser class [org.springframework.contex ...
- sc服务查询
sc query lanmanse2rver |find /i "state" |find /i "running" || ( echo 该服务没有运行时,要执 ...
- 10个顶级的CSS3代码生成器
新出来的在线工具和web应用允许开发人员快速创建网站,而无需手动一行一行地编写代码.当前,不断有新的框架和代码库涌现在前端开发这个领域里. 但是,这也让许多开发人员忘记了代码生成器以及它们在构建网站时 ...