'.' 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. QC的使用学习(二)

    今日学习清单: 1.Quality  Center中左上角选项中(QC 10.0中文版)工具菜单下的自定义中的几个内容,有:用户属性.组.项目用户.模块访问.需求类型.项目列表等.用户属性打开后是对当 ...

  2. extjs/js时间校验

    //时间秒判断var re=/^(?:19|20)[0-9][0-9]-(?:(?:0[1-9])|(?:1[0-2]))-(?:(?:[0-2][1-9])|(?:[1-3][0-1])) (?:( ...

  3. python 正则表达式 (重点) re模块

    京东的注册页面,打开页面我们就看到这些要求输入个人信息的提示.假如我们随意的在手机号码这一栏输入一个11111111111,它会提示我们格式有误.这个功能是怎么实现的呢?假如现在你用python写一段 ...

  4. vs code 代码格式化整理

    vs code格式化代码的快捷键如下:(来源于这里) On Windows Shift + Alt + F On Mac Shift + Option + F On Ubuntu Ctrl + Shi ...

  5. LeetCode - 1. Two Sum(8ms)

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  6. Drools 7.4.1.Final参考手册(八) 规则语言参考

    规则语言参考 概述 Drools有一个“本地”的规则语言.这种格式在标点符号上非常轻,并且通过“扩展器”支持自然语言和领域特定的语言,使语言能够变形到您的问题领域.本章主要与本机规则格式一致.用于表示 ...

  7. Java IO流-File类的使用示例-创建文件夹和文件的正确方法

    当创建一个文件时,比如:E:\\test\\test.txt,此时若文件夹test不存在,那么直接创建文件会出错,故首先要判断文件夹是否存在,不存在的话要首先创建文件夹. public class F ...

  8. lintcode-93-平衡二叉树

    93-平衡二叉树 给定一个二叉树,确定它是高度平衡的.对于这个问题,一棵高度平衡的二叉树的定义是:一棵二叉树中每个节点的两个子树的深度相差不会超过1. 您在真实的面试中是否遇到过这个题? Yes 样例 ...

  9. linux系统基础文件属性

    记录用户登录前显示的信息 cat /etc/issue vim  /etc/motd    设置登录提醒 隐藏执行命令的历史记录用history –d  加上历史记录行号 如history -d 38 ...

  10. Xcode & swift

    swift-apps swift 2018 apps Xcode Swift Playground https://developer.apple.com/download/ https://deve ...