题目

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;
}
}
};

GitHub测试程序源码

LeetCode(10) Regular Expression Matching的更多相关文章

  1. LeetCode(10)Regular Expression Matching

    题目如下: Python代码: # -*- coding:utf-8 -*- def ismatch(s,p): #先将dp[s+1][p+1]二维数组全置为False dp = [[False] * ...

  2. Leetcode(10)正则表达式匹配

    Leetcode(10)正则表达式匹配 [题目表述]: 给定一个字符串 (s) 和一个字符模式 (p).实现支持 '.' 和 '*' 的正则表达式匹配. '.' 匹配任意单个字符. '*' 匹配零个或 ...

  3. leetcode第十题--Regular Expression Matching

    Problem:Implement regular expression matching with support for '.' and '*'. '.' Matches any single c ...

  4. Leetcode(10)-正则表达式匹配

    给定一个字符串 (s) 和一个字符模式 (p).实现支持 '.' 和 '*' 的正则表达式匹配. '.' 匹配任意单个字符. '*' 匹配零个或多个前面的元素. 匹配应该覆盖整个字符串 (s) ,而不 ...

  5. LeetCode(10):正则表达式匹配

    Hard! 题目描述: 给定一个字符串 (s) 和一个字符模式 (p).实现支持 '.' 和 '*' 的正则表达式匹配. '.' 匹配任意单个字符. '*' 匹配零个或多个前面的元素. 匹配应该覆盖整 ...

  6. leetcode 10 Regular Expression Matching(简单正则表达式匹配)

    最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...

  7. 【leetcode】Regular Expression Matching (hard) ★

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  8. LeetCode OJ:Regular Expression Matching(正则表达式匹配)

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  9. [LeetCode] 10. Regular Expression Matching 正则表达式匹配

    Given an input string (s) and a pattern (p), implement regular expression matching with support for  ...

随机推荐

  1. hdu 2818 Building Block 种类并查集

    在进行并的时候不能瞎jb并,比如(x, y)就必须把x并给y ,即fa[x] = y #include <iostream> #include <string> #includ ...

  2. iOS UITextView自适应高度UITextContainerView抖动问题

    在打造一个类似于微信朋友圈评论输入框的时候,需要动态调整输入框的高度, 但是,在调整了UITextView的高度之后,继续输入会导致内容(UITextContainerView里的文字)抖动. scr ...

  3. Rsync 实现远程同步

    介绍 rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.rsync使用所谓的“rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部 ...

  4. WIN7 X64的运行命令窗口

    要在WIN7系统下用界面的方式执行运行命令,则可以用如下两种方法方法打开运行对话框.1.点Win+R(run)就能出来的.2.在开始菜单上点右键,选“属性”,进入开始菜单属性设置界面,单击底部的“自定 ...

  5. HDU - 6063 RXD and math

    Bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6063 打表发现规律是n^k #include <iostream> #inc ...

  6. AWVS11使用教程——Acunetix Web Vulnerability Scanner 11.x

    AWVS11使用教程 一:普通扫描. 二:报告生成. 三:登陆扫描. Acunetix Web Vulnerability Scanner(简称AWVS)是一款知名的网络漏洞扫描工具,它通过网络爬虫测 ...

  7. 135 Candy 分配糖果

    There are N children standing in a line. Each child is assigned a rating value.You are giving candie ...

  8. JVM内存配置参数-XMX,-XMS,-XMN的例子

    转载:http://www.nowcoder.com/questionTerminal/093bfa948d144ce3b0a68b938ae8b4ec 对于JVM内存配置参数: -Xmx10240m ...

  9. Web API 实体显示注释

    我看园子里关于Web API的注释都是关于方法的,并没有显示实体注释的方法,今天花了一些时间搞了一下 其实注释的显示就是根据类库的XML文档文件生成的. 首先你要将所用到的类库生成XML文档文件: 在 ...

  10. Scala基础篇-05求值策略

    Scala的求值策略有2种: call by value call by name 如何区分? 例子: def bar(x:Int,y: => Int) = def loop(): Int=lo ...