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
@首先题目要理解,通配符*是重复前面一个元素,而不是*前面所有的元素而且通配符*号前面必须要有元素,就是说*出现的位置不可能在第一位。
f[i][j] = f[i][j - ] || (s[i - ] == p[j - ] || '.' == p[j - ]) && f[i - ][j];

f[i][j - 2]表示前面的元素出现0次,后面表示出现次数大于等于1.

aabbb

aab.*

能够出现多次,说明s中减少一个(i -1)也能匹配,所以这个条件也必须满足。

s[i - 1] == p[j - 2]因为ij表示出现的元素个数,相当于下标从i - 1,j - 1.
表示p中倒数第二个元素要和s中倒数第一个元素相等。这样才能进行重复。
注意初始化第一列的情况。
class Solution {
public:
bool isMatch(string s, string p) {
if(s.size() == && p.size() == ){
return true;
}
int m = s.size();
int n = p.size();
vector<vector<bool>> dp(m + ,vector<bool> (n + ,false)); dp[][] = true;
for(int i = ;i <= m;++i){
dp[i][] = false;
}
for(int j = ;j <= n;++j){
if((j > ) && (j % == ) && dp[][j - ] && p[j - ] == '*'){
dp[][j] = true;
}
} for(int i = ;i <= m;++i){
for(int j = ;j<= n;++j){
if(p[j - ] != '*'){
dp[i][j] = dp[i - ][j - ] && (s[i - ] == p[j - ] || '.' == p[j - ]);
}
else{
dp[i][j] = dp[i][j - ] || dp[i - ][j] && ((s[i - ] == p[j - ]) || '.' == p[j - ]);
}
}
}
return dp[m][n];
}
};

10. Regular Expression Matching正则表达式匹配的更多相关文章

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

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

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

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

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

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

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

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

  5. 10. Regular Expression Matching字符串.*匹配

    [抄题]: Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

  6. 010 Regular Expression Matching 正则表达式匹配

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

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

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

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

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

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

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

随机推荐

  1. Python - 编程技巧,语法糖,黑魔法,pythonic

    参考,搬运 http://python-web-guide.readthedocs.io/zh/latest/idiom/idiom.html 待定 1. Python支持链式比较 # bad a = ...

  2. css3内外阴影同时显示

    内外阴影同时显示: box-shadow: 0px 0px 0.4rem rgba(255,255,255,0.5) inset,0px 0px 0.7rem rgba(185,119,143,0.9 ...

  3. Python 的直接赋值、Deepcopy、Copy的区别

    直接赋值: 其实就是对象的引用 浅拷贝(copy): 只拷贝符对象,不会拷贝对象内部的子对象 深拷贝(deepcopy): copy 模块的 deepcopy 方法,完全拷贝了父对象及其子对象 有图有 ...

  4. windows 以太坊开发框架Truffle环境搭建

    https://www.jianshu.com/p/f7a4de0cba9d 一.安装DApp开发环境 1.1 安装Node.js 我们使用官方长期支持的8.10.0LTS版本,下载64位包装包. 下 ...

  5. 学习笔记(14)- SQuAD的数据格式

    BERT模型完成问答任务的时候,需要数据格式为SQuAD形式. 有2个版本,1.1和2.0

  6. python中sys和os的区别

    <os和sys的官方解释> ➤os os: This module provides a portable way of using operating system dependent ...

  7. windows 批量杀进程 类似pkill

    轉:http://blog.sina.com.cn/s/blog_55fb522f0100whki.html 1.开始-运行(或win+R),输入cmd,打开命令行模式: 2.输入tasklist,可 ...

  8. centos7搭建svn服务器及客户端设置

    centos7搭建svn服务器及客户端设置 centos7貌似预装了svn服务(有待确认),因此我们直接启动该服务即可 一.svn服务端配置(服务器IP假设为192.168.100.1) 步骤1:创建 ...

  9. 搜索栏UISearchBar的使用

    本文结构: 1.首先是对UISearchBar的简介文字 2.初始化展现UISearchBar,并解析它的结构 3.属性.方法.代理等的一一介绍 4.日常的使用,包括单独对UISearchBar的配置 ...

  10. c#数据四种执行方法(ExecuteNonQuery)-----转载

    c#数据四种执行方法(ExecuteNonQuery)   1.使用ExecuteReader()操作数据库 2.使用ExecuteNonQuery()操作数据库 3.使用ExecuteScalar( ...