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
@首先题目要理解,通配符*是重复前面一个元素,而不是*前面所有的元素。而且通配符*号前面必须要有元素,就是说*出现的位置不可能在第一位。
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正则表达式匹配的更多相关文章
- [LeetCode] 10. Regular Expression Matching 正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- [LeetCode]10. Regular Expression Matching正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- [leetcode]10. Regular Expression Matching正则表达式的匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- [LeetCode] Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 10. Regular Expression Matching字符串.*匹配
[抄题]: Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...
- 010 Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'.'.' Matches any single character. ...
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- Leetcode 10. Regular Expression Matching(递归,dp)
10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...
- leetcode 10. Regular Expression Matching 、44. Wildcard Matching
10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...
随机推荐
- Win10 在 CUDA 10.1 下跑 TensorFlow 2.x
深度学习最热的两个框架是 pytorch 和 tensorflow,pytorch 最新版本是 1.3,tensorflow 最新版本为 2.0,在 win10 下 pytorch 1.3 要求的 c ...
- python时间序列按频率生成日期的方法
引用:https://www.zhangshengrong.com/p/281omE7rNw/ 有时候我们的数据是按某个频率收集的,比如每日.每月.每15分钟,那么我们怎么产生对应频率的索引呢?pan ...
- 树莓派安装nextcloud、Seafile
参考博文:http://bbs.eeworld.com.cn/thread-505579-1-1.html http://blog.sina.com.cn/s/blog_6f55d8210102xia ...
- Spring学习(二)
IoC 1.Inverse of Control ,控制反转(控制权的翻转) 2.控制:对对象的创建.对对象的属性赋值等一系列操作本来应该是我们做的事情 Java Application : Date ...
- 正确使用 Android 的 Theme 和 Style
原文:http://www.tuicool.com/articles/ZjEZFj Android 5.0 可以给一个 View 单独设置一个 theme 了,其主要用途就是用在 ToolBar 上, ...
- Spring Mvc中Jsp也页面怎么会获取不到Controller中的数据
----------Controller ------- package com.test.mvc; import org.springframework.stereotype.Controller; ...
- python人脸对比
import sys import ssl from urllib import request,parse # client_id 为官网获取的AK, client_secret 为官网获 ...
- linux的ls -al指令
ls是“list”的意思,参数-al则表示列出所有的文件,包括隐藏文件,就是文件前面第一个字符为.的文件. 1.第一列便是这个文件的属性: #第一个属性表示这个文件时“目录.文件或链接文件等”: ...
- 组态DP主站与标准从站的步骤
分为以下几个部分 第一:组态DP主站与标准从站 分为以下几个步骤 步骤1: 将标准从站ET200 ,ET200在硬件组态软件界面的最右边的PROFIBUS-DP界面里面, PROFIBUS-DP里面是 ...
- 第1节 storm编程:7、并行度分析以及如何解决线程安全问题
storm其实就是一个多进程与多线程的框架 开多个进程:分配到的资源更多 开多个线程:执行的速度更快 设置进程个数以及线程个数 ==================================== ...