正则表达式匹配

题目描述:给你一个字符串 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-正则表达式匹配的更多相关文章

  1. Leetcode 10. 正则表达式匹配 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  2. Java实现 LeetCode 10 正则表达式匹配

    10. 正则表达式匹配 给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 '.' 和 '*' 的正则表达式匹配. '.' 匹配任意单个字符 '*' 匹配零个或多个前面的那一个元素 所谓匹配, ...

  3. [LeetCode] 10. 正则表达式匹配

    题目链接:https://leetcode-cn.com/problems/regular-expression-matching/ 题目描述: 给定一个字符串 (s) 和一个字符模式 (p).实现支 ...

  4. 【LeetCode】正则表达式匹配(动态规划)

    题目描述 给定一个字符串 (s) 和一个字符模式 (p).实现支持 '.' 和 '*' 的正则表达式匹配. '.' 匹配任意单个字符. '*' 匹配零个或多个前面的元素. 匹配应该覆盖整个字符串 (s ...

  5. LeetCode 10. 正则表达式匹配(Regular Expression Matching)

    题目描述 给定一个字符串 (s) 和一个字符模式 (p).实现支持 '.' 和 '*' 的正则表达式匹配. '.' 匹配任意单个字符. '*' 匹配零个或多个前面的元素. 匹配应该覆盖整个字符串 (s ...

  6. LeetCode 10——正则表达式匹配

    1. 题目 2. 解答 在 回溯算法 中我们介绍了一种递归的思路来求解这个问题. 此外,这个问题也可以用动态规划的思路来解决.我们定义状态 \(P[i][j]\) 为子串 \(s[0, i)\) 和 ...

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

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

  8. LeetCode(10):正则表达式匹配

    Hard! 题目描述: 给定一个字符串 (s) 和一个字符模式 (p).实现支持 '.' 和 '*' 的正则表达式匹配. '.' 匹配任意单个字符. '*' 匹配零个或多个前面的元素. 匹配应该覆盖整 ...

  9. [LeetCode][Facebook面试题] 通配符匹配和正则表达式匹配,题 Wildcard Matching

    开篇 通常的匹配分为两类,一种是正则表达式匹配,pattern包含一些关键字,比如'*'的用法是紧跟在pattern的某个字符后,表示这个字符可以出现任意多次(包括0次). 另一种是通配符匹配,我们在 ...

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

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

随机推荐

  1. Python初学笔记之可变类型、不可变类型

    python中 可变类型: 列表 list 字典 dict 不可变类型: 数字型:int.float.complex.bool.long 字符型 str 元组 tuple id(i):通过id查看变量 ...

  2. Linux配置Redis集群 和 缓存介绍。

    // 一.什么是缓存? mybatis一级缓存和二级缓存 mybatis的一级缓存存在哪? SqlSession,就不会再走数据库 什么情况下一级缓存会失效? 当被更新,删除的时候sqlsession ...

  3. Spring源码-AOP部分-Spring是如何对bean实现AOP代理的

    实验环境:spring-framework-5.0.2.jdk8.gradle4.3.1 历史文章 Spring源码-IOC部分-容器简介[1] Spring源码-IOC部分-容器初始化过程[2] S ...

  4. String Reversal

    Educational Codeforces Round 96 (Rated for Div. 2) - E. String Reversal 跳转链接 题目描述 定义一个操作为交换字符串中相邻的两个 ...

  5. 实际工程中加快 Java 代码编写的小提示

    这里我简单谈谈 Java 语法在编程效率方面的弱势,以及如何补救. 一.集合的快速创建 C# 是少数拥有集合字面值(又叫初始化表达式)的静态语言之一. var list = new List<i ...

  6. X000101

    P3879 [TJOI2010]阅读理解 考虑用 Trie 解决 #include<stdio.h> #include<bitset> #include<string.h ...

  7. Android的基本资源引用(字符串、颜色、尺寸、数组)【转】

    感谢大佬:https://blog.csdn.net/wenge1477/article/details/81295763 Android的基本资源引用(字符串.颜色.尺寸.数组)[转] 一.Andr ...

  8. 配置phpmemcache扩展,Loaded Configuration File (none)

    首先我来描述问题: 编译安装完php的扩展库memcache后,在php.ini文件中添加了memcache.so的配置文件 extension=/usr/local/php5.6.27/lib/ph ...

  9. iOS中属性 (nonatomic, copy, strong, weak)的使用 By hL

    以下内容来自Stackflow的详解 1.Nonatomicnonatomic is used for multi threading purposes. If we have set the non ...

  10. java创建一个子类对象是会调用父类的构造方法会不会创建父类

    1.子类在创建实例后,类初始化方法会调用父类的初始化方法(除了Java.lang.Object类,因为java.lang.Object类没有父类),而这种调用会逐级追述,直到java.lang.Obj ...