'.' 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

对于我这种算法菜鸟都不算的门外汉,自然写出了又长又臭的if else考虑了一大堆的case,结果还是各种bug各种case通不过。。。醉了,早好好学习也不至于现在这么狼狈,还好不是太晚。

最终参考了http://blog.csdn.net/hopeztm/article/details/7992253浙大牛人的思路加代码

这题主要运用动态规划(DP):(这里用s指向源串,p指向待匹配串)

跟上题回文匹配类似使用dp[i][j]表示s[0...strlen(s)]和p[0...strlen(p)]的匹配情况

if  *(p+1)  != '*'

if *p == *s       dp[i][j]  = dp[i+1][j+1]

  else return false

if *(p+1) == '*'   这是需要考虑拓展.*的情况来匹配,并且每一步都要增加s,如果匹配返回true,否则返回通配符匹配后的结果。

这里面由dp[i][j]到dp[i+1][j+1]就是迭代的过程,后面每次增加s检查匹配也是迭代,若增加s的过程无匹配,则退回到最开始通配符匹配环节。

LeetCode 10 Regular Expression Match的更多相关文章

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

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

  2. Leetcode 10. Regular Expression Matching(递归,dp)

    10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...

  3. leetcode 10. Regular Expression Matching 、44. Wildcard Matching

    10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...

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

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

  5. LeetCode (10): Regular Expression Matching [HARD]

    https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...

  6. [leetcode]10. Regular Expression Matching正则表达式的匹配

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

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

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

  8. [LeetCode] 10. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...

  9. Java [leetcode 10] Regular Expression Matching

    问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...

随机推荐

  1. spring location设置本地路径

    <context:property-placeholder location="file:D:/jdbc.properties"/> 直接在路径前加上 file:

  2. idea在Maven Projects中显示灰色的解决办法

    问题描述: 在使用idea的过程中,遇到其中一个maven模块变成灰色,如下所示: 问题解决: 造成这个的原因可能是忽略了maven模块. 可以尝试如下解决方法:在idea中进入Settings–&g ...

  3. JMeter学习笔记(九) 参数化3--User Defined Variables

    3.User Defined Variables 1)添加用户定义的变量 2)添加变量 3)添加HTTP请求,引用变量,格式:${} 4)执行HTTP请求,察看结果树 5)用户定义的变量,优缺点: * ...

  4. python 基础篇 14 程程器表达式 内置函数

    昨日内容回顾    可迭代对象:        内部含有__iter__方法的就是可迭代对象.        可迭代对象不能取值,因为内部不含有__next__方法.     可迭代对象 ---> ...

  5. Spring实战第九章学习笔记————保护Web应用

    保护Web应用 在这一章我们将使用切面技术来探索保护应用程序的方式.不过我们不必自己开发这些切面----我们将使用Spring Security,一种基于Spring AOP和Servlet规范的Fi ...

  6. ardupilot_gazebo仿真(一)

    ardupilot_gazebo仿真 标签(空格分隔): 未分类 ardupilot_gazebo仿真 官网网址 代码更新地址 Ardupilot Gazebo Plugin & Models ...

  7. [GraphSAGE] docker安装与程序运行

    安装Docker与程序运行 1. requirements.txt Problem: Downloading https://files.pythonhosted.org/packages/69/cb ...

  8. PyTorch深度学习计算机视觉框架

    Taylor Guo @ Shanghai - 2018.10.22 - 星期一 PyTorch 资源链接 图像分类 VGG ResNet DenseNet MobileNetV2 ResNeXt S ...

  9. BZOJ 2049 SDOI2008 洞穴勘测 LCT板子

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2049 题意概述:给出N个点,一开始不连通,M次操作,删边加边,保证图是一个森林,询问两点连 ...

  10. Mac下使用Charles抓包Android

    原文地址:http://fanjiajia.cn/2018/11/21/Mac%E4%B8%8B%E4%BD%BF%E7%94%A8Charles%E6%8A%93%E5%8C%85Android/ ...