LeetCode-010-正则表达式匹配
正则表达式匹配
题目描述:给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 '.' 和 '*' 的正则表达式匹配。
'.' 匹配任意单个字符
'*' 匹配零个或多个前面的那一个元素
所谓匹配,是要涵盖 整个 字符串 s的,而不是部分字符串。示例说明请见LeetCode官网。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/regular-expression-matching/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解法一:遍历、递归
遍历p,将s和p进行匹配。分几种情况,分别是 '.' 或 '*' 或者两者都不是的情况, '.' 和两者都不是的相对比较简单,比较复杂点的是'**'的判断,因为'**'是匹配零个或多个元素,所以用到了递归。
public class Solution {
public static boolean isMatch(String s, String p) {
if ((s == null || s.length() == 0) && (p == null || p.length() == 0)) {
return true;
}
if ((p == null || p.length() == 0) && s.length() > 0) {
return false;
}
char preChar = 0;
boolean havePreChar = false;
int sIndex = 0;
for (int i = 0; i < p.length(); i++) {
if (p.charAt(i) == '.') {
if (i < p.length() - 1 && p.charAt(i + 1) == '*') {
preChar = p.charAt(i);
havePreChar = true;
continue;
} else {
if (sIndex < s.length()) {
preChar = s.charAt(sIndex);
sIndex++;
havePreChar = true;
continue;
} else {
return false;
}
}
}
if (p.charAt(i) == '*') {
if (!havePreChar) {
return false;
}
if (sIndex == s.length()) {
if (p.length() - 1 == i) {
return true;
} else {
return isMatch("", p.substring(i + 1));
}
} else {
if (preChar == '.') {
if (i == p.length() - 1) {
return true;
}
while (sIndex < s.length()) {
boolean result = isMatch(s.substring(sIndex), p.substring(i + 1));
if (result) {
return true;
}
sIndex++;
continue;
}
} else {
while (sIndex < s.length()) {
if (s.charAt(sIndex) == preChar) {
boolean result = isMatch(s.substring(sIndex), p.substring(i + 1));
if (result) {
return true;
}
sIndex++;
continue;
} else {
break;
}
}
}
havePreChar = false;
}
}
if (p.charAt(i) != '*' && p.charAt(i) != '.') {
if (i < p.length() - 1 && p.charAt(i + 1) == '*') {
preChar = p.charAt(i);
havePreChar = true;
continue;
} else {
if (sIndex == s.length() || s.charAt(sIndex) != p.charAt(i)) {
return false;
} else {
sIndex++;
havePreChar = false;
continue;
}
}
}
}
if (sIndex < s.length()) {
return false;
}
return true;
}
public static void main(String[] args) {
String s = "", p = "c*c*"; // true
System.out.println(isMatch(s, p));
}
}
LeetCode-010-正则表达式匹配的更多相关文章
- Leetcode 10. 正则表达式匹配 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- Java实现 LeetCode 10 正则表达式匹配
10. 正则表达式匹配 给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 '.' 和 '*' 的正则表达式匹配. '.' 匹配任意单个字符 '*' 匹配零个或多个前面的那一个元素 所谓匹配, ...
- [LeetCode] 10. 正则表达式匹配
题目链接:https://leetcode-cn.com/problems/regular-expression-matching/ 题目描述: 给定一个字符串 (s) 和一个字符模式 (p).实现支 ...
- 【LeetCode】正则表达式匹配(动态规划)
题目描述 给定一个字符串 (s) 和一个字符模式 (p).实现支持 '.' 和 '*' 的正则表达式匹配. '.' 匹配任意单个字符. '*' 匹配零个或多个前面的元素. 匹配应该覆盖整个字符串 (s ...
- LeetCode 10. 正则表达式匹配(Regular Expression Matching)
题目描述 给定一个字符串 (s) 和一个字符模式 (p).实现支持 '.' 和 '*' 的正则表达式匹配. '.' 匹配任意单个字符. '*' 匹配零个或多个前面的元素. 匹配应该覆盖整个字符串 (s ...
- LeetCode 10——正则表达式匹配
1. 题目 2. 解答 在 回溯算法 中我们介绍了一种递归的思路来求解这个问题. 此外,这个问题也可以用动态规划的思路来解决.我们定义状态 \(P[i][j]\) 为子串 \(s[0, i)\) 和 ...
- [LeetCode] Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- LeetCode(10):正则表达式匹配
Hard! 题目描述: 给定一个字符串 (s) 和一个字符模式 (p).实现支持 '.' 和 '*' 的正则表达式匹配. '.' 匹配任意单个字符. '*' 匹配零个或多个前面的元素. 匹配应该覆盖整 ...
- [LeetCode][Facebook面试题] 通配符匹配和正则表达式匹配,题 Wildcard Matching
开篇 通常的匹配分为两类,一种是正则表达式匹配,pattern包含一些关键字,比如'*'的用法是紧跟在pattern的某个字符后,表示这个字符可以出现任意多次(包括0次). 另一种是通配符匹配,我们在 ...
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
随机推荐
- django之集成第三方支付平台PaysAPI与百度云视频点播服务接入
PaysAPI直接查看接口文档:https://www.paysapi.com/docindex,比较简单 百度云视频点播服务接入: 1. 准备工作:百度云的示例:http://cyberplayer ...
- Entity Framework 在OrderBy排序中使用字符串
public static class LinqExtensions { private static PropertyInfo GetPropertyInfo(Type objType, strin ...
- js instanceof 解析
js中的instanceof运算符 概述 instanceof运算符用来判断一个构造函数的prototype属性所指向的对象是否存在另外一个要检测对象的原型链上 语法 obj instanceofOb ...
- redis中scan和keys的区别
scan和keys的区别 redis的keys命令,通来在用来删除相关的key时使用,但这个命令有一个弊端,在redis拥有数百万及以上的keys的时候,会执行的比较慢,更为致命的是,这个命令会阻塞r ...
- Cell的重用原理
iOS设备的内存有限,如果用UITableView显示成千上万条数据,就需要成千上万个UITableViewCell对象的话,那将会耗尽iOS设备的内存.要解决该问题,需要重用UITableViewC ...
- SqlServer数据库表生成C# Model实体类SQL语句——补充
在sql语句最前边加上 use[数据库名] 原链接:https://www.cnblogs.com/jhli/p/11552105.html --[SQL骚操作]SqlServer数据库表生成C ...
- Java线程--CopyOnWrite容器使用
原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11871602.html Java线程--CopyOnWrite容器使用 CopyOnWrit容 ...
- 深入详解Mybatis的架构原理与6大核心流程
MyBatis 是 Java 生态中非常著名的一款 ORM 框架,目前在一线互联网大厂中应用广泛,Mybatis已经成为了一个必会框架. 如果你想要进入一线大厂,能够熟练使用 MyBatis 开发已经 ...
- python小白记录一 ——python脚本生成windows可执行exe
1.需要安装pywin32 先查看自己有没有安装:使用如下命令查看 pip show pywin32 如果没有则用下面方式进行安装: pip install pywin32 然后等待安装完成: 2.再 ...
- 4、架构--NFS实践、搭建web服务、文件共享
笔记 1.晨考 1.数据备份的方式有哪些 全量和增量 2.数据备份的命令有哪些,都有哪些优点缺点 cp : 本地,全量复制 scp :远程,全量复制 rsync :远程,增量复制 3.rsync的参数 ...