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 ... 
随机推荐
- ApacheCN NodeJS 译文集 20211204 更新
			Node API 开发入门指南 零.前言 一.Node.js 简介 二.构建 API--第 1 部分 三.构建 API--第 2 部分 React TypeScript Node 全栈开发 零.序言 ... 
- Disruptor-高性能队列
			简介 Disruptor是英国外汇交易公司LMAX开发的一个高性能队列,研发的初衷是解决内存队列的延迟问题.与Kafka.RabbitMQ用于服务间的消息队列不同,disruptor一般用于线程间消息 ... 
- Td 内容不换行,超过部分自动截断,用...表示
			转载请注明来源:https://www.cnblogs.com/hookjc/ <table width="200px" style="table-layout:f ... 
- 通过导入Jar包的方式使用JSONObject
			如果想要在Java中使用JSONObject,而且只想通过导入jar包的方式下,那么仅仅导入Json的jar包还是不够的. 不然会报:java.lang.ClassNotFoundException: ... 
- PHP版的猴子选大王算法
			猴子选大王 这个算法可能是目前我看到的最简洁都算法吧,而且很好理解.它不同于其他算法,其他算法都是判断这个猴子能不能被选中,而他只是找出不能被选中的猴子,然后将其塞到数组模拟的环状队列中,参与下次选. ... 
- 一个好用的多方隐私求交算法库JasonCeng/MultipartyPSI-Pro
			Github链接传送:JasonCeng/MultipartyPSI-Pro 大家好,我是阿创,这是我的第29篇原创文章. 今天是一篇纯技术性文章,希望对工程狮们有所帮助. 向大家推荐一个我最近改造的 ... 
- Centos7系统使用yum遇到的问题failure: repodata/repomd.xml from base: [Errno 256] No more mirrors to try.
			简单粗暴重新安装yum. 1.查看linux上所有的yum包 # rpm -qa|grep yum 2.逐个卸载,如 # rpm -e yum-plugin-fastestmirror-1.1.31- ... 
- Solution -「多校联训」战神归来
			\(\mathcal{Description}\) Link. 一条地铁线路上共 \(m\) 个站点,\(n\) 个人乘坐地铁,第 \(i\) 个人需要从 \(s_i\) 站坐到 \(e_i\ ... 
- Solution -「Tenka1 2019 D」Three Colors
			\(\mathcal{Description}\) Link. 给定 \(\{a_n\}\),把每个元素划分入可重集 \(R,G,B\) 中的恰好一个,求满足 \(\sum R,\sum G, ... 
- 轻量级DI框架Guice使用详解
			背景 在日常写一些小工具或者小项目的时候,有依赖管理和依赖注入的需求,但是Spring(Boot)体系作为DI框架过于重量级,于是需要调研一款微型的DI框架.Guice是Google出品的一款轻量级的 ... 
