乘风破浪:LeetCode真题_010_Regular Expression Matching

一、前言

关于正则表达式我们使用得非常多,但是如果让我们自己写一个,却是有非常大的困难的,我们可能想到状态机,确定,非确定状态机确实是一种解决方法,不过需要消耗很大的时间去推理和计算,对于正则表达式的缩小版,我们往往可以通过递归,递推,动态规划等方法来解决。

二、Regular Expression Matching

2.1 问题理解

2.2 问题分析和解决

    遇到这样的问题,我们想到了递归,对于.是很好处理和匹配的,但是如果和*结合起来就变化无穷了,正是因为*我们才要递归。

    让我们看看官方的答案:

class Solution {
public boolean isMatch(String text, String pattern) {
if (pattern.isEmpty()) return text.isEmpty();
boolean first_match = (!text.isEmpty() &&
(pattern.charAt(0) == text.charAt(0) || pattern.charAt(0) == '.')); if (pattern.length() >= 2 && pattern.charAt(1) == '*'){
return (isMatch(text, pattern.substring(2)) ||
(first_match && isMatch(text.substring(1), pattern)));
} else {
return first_match && isMatch(text.substring(1), pattern.substring(1));
}
}
}

    如果模式串和源串第一个字符能够正常匹配,并且不为空,模式串的第二个字符不为'*',那么我们可以继续递归匹配下面的东西:

 return first_match && isMatch(text.substring(1), pattern.substring(1));

    如果模式串的长度大于1,并且第二个字符是*,那么我们就有可能匹配到源串的很多的字符,也就相当于将源串已经匹配的去掉,拿剩下的和整个模式串继续比较,此时*发挥了作用,或者比较源串与去掉了*的模式串,因为*没有能够发挥作用。于是就得到了:

         if (pattern.length() >= 2 && pattern.charAt(1) == '*'){
return (isMatch(text, pattern.substring(2)) ||
(first_match && isMatch(text.substring(1), pattern)));
}

     除此之外我们还可以使用动态规划算法:

class Solution {
public boolean isMatch(String text, String pattern) {
boolean[][] dp = new boolean[text.length() + 1][pattern.length() + 1];
dp[text.length()][pattern.length()] = true; for (int i = text.length(); i >= 0; i--){
for (int j = pattern.length() - 1; j >= 0; j--){
boolean first_match = (i < text.length() &&
(pattern.charAt(j) == text.charAt(i) ||
pattern.charAt(j) == '.'));
if (j + 1 < pattern.length() && pattern.charAt(j+1) == '*'){
dp[i][j] = dp[i][j+2] || first_match && dp[i+1][j];
} else {
dp[i][j] = first_match && dp[i+1][j+1];
}
}
}
return dp[0][0];
}
}

     首先我们定义dp[i][j]代表源串T[i:]和模式串P[j:]是匹配的,其中i,j为源串和模式串的下标,于是我们只要求得dp[0][0]的值就可以了。我们已知的条件是:

 dp[text.length()][pattern.length()] = true;

  于是我们从后往前倒求最终的dp[0][0],通过如下的判断,看看是哪一种情况,然后根据相应的情况采取不同的递推策略,最终得到结果:

    boolean first_match = (i < text.length() &&
(pattern.charAt(j) == text.charAt(i) ||
pattern.charAt(j) == '.'));
if (j + 1 < pattern.length() && pattern.charAt(j+1) == '*'){
dp[i][j] = dp[i][j+2] || first_match && dp[i+1][j];
} else {
dp[i][j] = first_match && dp[i+1][j+1];
}

同样的我们算法也是使用了递归和动态规划:

    在动态规划方面我们使用match[i]来表示对于源串从i到最后(T[i:])都是能够匹配的,于是之用求match[0]即可。

import java.util.Arrays;

public class Solution {
/**
* Implement regular expression matching with support for '.' and '*'.
* '.' Matches any single character.
* '*' Matches zero or more of the preceding element.
*
* 题目大意:
* 实现一个正则表达式匹配算法,.匹配任意一个字符,*匹配0个或者多个前导字符
*/
public boolean isMatch(String s, String p) {
boolean[] match = new boolean[s.length() + 1];
Arrays.fill(match, false);
match[s.length()] = true;//刚开始满足需要
for (int i = p.length() - 1; i >= 0; i--) {
if (p.charAt(i) == '*') {
for (int j = s.length() - 1; j >= 0; j--) {
          //原来就是false只有能够为真,才为真。
match[j] = match[j] || match[j + 1]
&& (p.charAt(i - 1) == '.' || s.charAt(j) == p.charAt(i - 1));

}
i--;
} else {
for (int j = 0; j < s.length(); j++) {
//从前往后,只有到了已经有true的时候才能生效。如果从后往前反而有问题。
match[j] = match[j + 1]
&& (p.charAt(i) == '.' || p.charAt(i) == s.charAt(j));

}
          //将最后的置为假,本来就应该不真,便于以后的判断
match[s.length()] = false;
}
}
return match[0];
} // 下面的代码用时比较长
public boolean isMatch2(String s, String p) {
// 输入都为null
if (s == null && p == null) {
return true;
}
// 有一个为null
else if (s == null || p == null) {
return false;
} return isMatch(s, 0, p, 0);
} /**
* 正则表达式匹配
*
* @param s 匹配串
* @param sIdx 当前匹配的位置
* @param p 模式串
* @param pIdx 模式串的匹配位置
* @return 匹配结果
*/
public boolean isMatch(String s, int sIdx, String p, int pIdx) {
// 同时到各自的末尾
if (s.length() == sIdx && p.length() == pIdx) {
return true;
}
// 当匹配串没有到达末尾,模式串已经到了末尾
else if (s.length() != sIdx && p.length() == pIdx) {
return false;
}
// 其它情况
else {
// 如果当前匹配的下一个字符是*号
if (pIdx < p.length() - 1 && p.charAt(pIdx + 1) == '*') {
// 匹配串未结束并且当前字符匹配(字符相等或者是.号)
if (sIdx < s.length() && (s.charAt(sIdx) == p.charAt(pIdx) || p.charAt(pIdx) == '.')) {
return isMatch(s, sIdx + 1, p, pIdx + 2) // 匹配串向前移动一个字符(只匹配一次)
|| isMatch(s, sIdx + 1, p, pIdx) // 匹配串向前移动一个字符(下一次匹配同样的(模式串不动))
|| isMatch(s, sIdx, p, pIdx + 2); // 忽略匹配的模式串
} else {
// 忽略*
return isMatch(s, sIdx, p, pIdx + 2);
}
} // 匹配一个字符
if (sIdx < s.length() && (s.charAt(sIdx) == p.charAt(pIdx) || p.charAt(pIdx) == '.')) {
return isMatch(s, sIdx + 1, p, pIdx + 1);
}
} return false;
} }

    如下表所示,使用递归需要1163ms而使用动态规划需要20ms,差别非常显著。

三、总结

对于一些比较困难的问题,我们需要从不同的角度考虑,解决问题的方法可以从递归,递推,动态规划等方面去考虑。

乘风破浪:LeetCode真题_010_Regular Expression Matching的更多相关文章

  1. 乘风破浪:LeetCode真题_020_Valid Parentheses

    乘风破浪:LeetCode真题_020_Valid Parentheses 一.前言 下面开始堆栈方面的问题了,堆栈的操作基本上有压栈,出栈,判断栈空等等,虽然很简单,但是非常有意义. 二.Valid ...

  2. 乘风破浪:LeetCode真题_041_First Missing Positive

    乘风破浪:LeetCode真题_041_First Missing Positive 一.前言 这次的题目之所以说是难,其实还是在于对于某些空间和时间的限制. 二.First Missing Posi ...

  3. 乘风破浪:LeetCode真题_040_Combination Sum II

    乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...

  4. 乘风破浪:LeetCode真题_039_Combination Sum

    乘风破浪:LeetCode真题_039_Combination Sum 一.前言     这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...

  5. 乘风破浪:LeetCode真题_038_Count and Say

    乘风破浪:LeetCode真题_038_Count and Say 一.前言     这一道题目,很类似于小学的问题,但是如果硬是要将输入和结果产生数值上的联系就会产生混乱了,因此我们要打破思维定势. ...

  6. 乘风破浪:LeetCode真题_037_Sudoku Solver

    乘风破浪:LeetCode真题_037_Sudoku Solver 一.前言 这次我们对于上次的模型做一个扩展并求解. 二.Sudoku Solver 2.1 问题 2.2 分析与解决     这道题 ...

  7. 乘风破浪:LeetCode真题_036_Valid Sudoku

    乘风破浪:LeetCode真题_036_Valid Sudoku 一.前言 有的时候对于一些基础知识的掌握,对我们是至关重要的,比如ASCII重要字符的表示,比如一些基本类型的长度. 二.Valid ...

  8. 乘风破浪:LeetCode真题_035_Search Insert Position

    乘风破浪:LeetCode真题_035_Search Insert Position 一.前言 这次的问题比较简单,也没有限制时间复杂度,但是要注意一些细节上的问题. 二.Search Insert ...

  9. 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array

    乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...

随机推荐

  1. 介绍nodejs中的path模块的几个方法

    webpack中常用的: var path = require('path') 是nodejs中的path模块,介绍一下webpack中常用的几个path模块的方法: 应用node环境的时候,这个pa ...

  2. WPF设置动画在控件载入时就立刻执行

    <YourControl.Triggers> <EventTrigger RoutedEvent="YourControl.Loaded"><!--这 ...

  3. java几个经典的算法题目----------查询子串和等于已知数字

    给出一个排序好的数组和一个数,求数组中连续元素的和等于所给数的子数组 public class testClockwiseOutput { public static void main(String ...

  4. Difference between $.ajax() and $.get() and $.load()

    转自:Difference between $.ajax() and $.get() and $.load() $.ajax() is the most configurable one, where ...

  5. Visual Studio 2017 远程调试(Remote Debugger)应用

    I.远程调试情景 项目部署在远程服务器或非本地环境中,需要 处理应用中遇到的一些错误时 (不能直接附加进程或F5调试应用). II. 远程调试准备 1.远程服务器--操作系统和硬件要求 MSDN 操作 ...

  6. Charles 抓取 iphone https的设置方式

    1. Charles:  help > SSL Proxying > Install Charles Root Certificate, 2. 将会打开 钥匙串访问 的功能,查找 Char ...

  7. java并发编程(7)构建自定义同步工具及条件队列

    构建自定义同步工具 一.通过轮询与休眠的方式实现简单的有界缓存 public void put(V v) throws InterruptedException { while (true) { // ...

  8. sublime text3 破解及常用插件

    sublime text3 下载 破解 submelime Text > About sublime Text //看是否注册并 查看当前的版本 然后百度或google搜索 'sublime t ...

  9. php访问mysql数据库的步骤

    官方说5.5开始就废弃mysql_query()这块东西很多,当然常用的就几个还是看手册吧. 这里简单记录一下.在我失忆之后可以找回一点记忆.最近一直用框架原生的都快忘了. 1.打开mysql连接 $ ...

  10. 撩课-Python-每天5道面试题-第1天

    一. 尽可能详细的描述出一个应用软件, 比如QQ, 在计算机中运行时涉及的软硬件, 以及说明我们编程的侧重点? 电脑开机, 从硬盘中(外部存储设备)加载操作系统(系统软件)到内存(内部存储设备), 并 ...