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 ...
随机推荐
- 【协作式原创】查漏补缺之Golang中mutex源码实现
概览最简单版的mutex(go1.3版本) 预备知识 主要结构体 type Mutex struct { state int32 // 指代mutex锁当前的状态 sema uint32 // 信号量 ...
- javaweb使用button的onclick属性访问servlet
1.定义一个servlet: 如我定义了一个名称为Choose_class.java的servlet 2.定义一个button <input type="button" v ...
- @implementer,抽象类,接口
@implementer,抽象类,接口 1. implementer 在看twisted源码时,经常出现@implementer(IReactorFDSet) 它来自zope.interfa ...
- Maven打包项目失败;报错:Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war (default-war) on project Hello: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/we
报错信息: E:\MIKEY\mikey\HTML5\TestMaven_01>mvn package [INFO] Scanning for projects... [INFO] [INFO] ...
- Java基础 -3.5
我觉得上一篇不是很严谨啊 我认为这个逻辑还是正确的 原码.反码.补码: (1)在Java中,所有数据的表示方式都是以补码形式来表示 如果没有特别的说明,Java 中的数据类型默认为int,int数据类 ...
- py related issues
在python中安装包出现Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) pip inst ...
- Unity表面着色器
表面着色器和之前无光照着色器不同,其中没有顶点着色器和片元着色器,而增加了光照函数: 接下写了一个求两个贴图的光照效果 两个贴图做插值运算: Shader "Custom/SurfaceSh ...
- 34 java 文件过滤 FileFilter
package com.da.tool.util; import org.apache.commons.io.filefilter.RegexFileFilter; import java.io.Fi ...
- Debug运行项目时报错,connected to the target VM, address: '127.0.0.1:50495', transport: 'socket'
Debug运行项目时报错,无法进入Debug,猜想出错原因可能是未正确关闭IDEA. 解决方法,先直接运行项目,然后停掉项目,再用Debug模式启动,问题解决.
- Scrapy 中的 Request 对象和 Respionse 对象
1.Request 对象 Request 对象用来描述一个 HTTP 请求,下面是其构造方法的参数列表 Request(url, [, callback, method='Get', headers, ...