问题描述:

Implement regular expression matching with support for '.' and '*'.

'.' Matches any single character.
'*' Matches zero or more of the preceding element.
http://i.cnblogs.com/EditPosts.aspx?opt=1
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

解题思路:

从字符串s和p的位置sp,pp处分别开始匹配,运用递归的方法不断缩小字符串的比较长度,最后回溯回来比较结果。

假设现在走到分别走到sp,pp处。则分为下面几种情况:

(1)如果pp的位置已经越界,那么若sp也已越界,那么则返回true;否则说明s字符串还有长度,不匹配,返回false;

(2)pp的位置为p的最后一位,那么此时只能一一匹配,若不匹配则返回false;否则进行下一位比较;

(3)pp的位置比较靠前,那么比较pp的下一位。如果下一位不是'*',则只能一一匹配,若不匹配则返回false;否则进行下一位比较。如果下一位是'*',则将pp的位置向后移动两位,sp的位置从现在开始进行暴力比较,每次向后移一位进行递归比较。

代码如下:

public class Solution {
public static boolean isMatch(String s, String p) {
if (s == null)
return p == null;
if (p == null)
return s == null;
return helper(s, p, 0, 0);
}
private static boolean helper(String s, String p, int sp, int pp) {
if (pp >= p.length())
return sp >= s.length();
// pp comes to end
if (pp == p.length() - 1) {
if (sp >= s.length()
|| (s.charAt(sp) != p.charAt(pp) && p.charAt(pp) != '.'))
return false;
else
return helper(s, p, sp + 1, pp + 1);
}
// p.charAt(pp+1)!='*'
if (p.charAt(pp + 1) != '*') {
if (sp >= s.length()
|| (s.charAt(sp) != p.charAt(pp) && p.charAt(pp) != '.'))
return false;
else
return helper(s, p, sp + 1, pp + 1);
}
// p.charAt(pp+1)=='*'
while (sp < s.length()
&& (p.charAt(pp) == '.' || s.charAt(sp) == p.charAt(pp))) {
if (helper(s, p, sp, pp + 2))
return true;
sp++;
}
return helper(s, p, sp, pp + 2);
}
}

Java [leetcode 10] Regular Expression Matching的更多相关文章

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

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

  2. Leetcode 10. Regular Expression Matching(递归,dp)

    10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...

  3. leetcode 10. Regular Expression Matching 、44. Wildcard Matching

    10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...

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

    Given an input string (s) and a pattern (p), implement regular expression matching with support for  ...

  5. LeetCode (10): Regular Expression Matching [HARD]

    https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...

  6. 蜗牛慢慢爬 LeetCode 10. Regular Expression Matching [Difficulty: Hard]

    题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single charac ...

  7. [LeetCode] 10. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...

  8. [leetcode]10. Regular Expression Matching正则表达式的匹配

    Given an input string (s) and a pattern (p), implement regular expression matching with support for  ...

  9. [LeetCode] 10. Regular Expression Matching ☆☆☆☆☆

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

随机推荐

  1. 辛星Spring4.x教程开放下载了

    下载地址:  https://pan.baidu.com/s/1kVSAYeb

  2. .htaccess 设置

     RewriteEngine on RewriteCond %{HTTP_HOST} ^blog.chosenet.com$RewriteCond %{REQUEST_URI} !^/blog/Rew ...

  3. Linux安装oracle 10g常见问题之——OUI-25031

    OUI-25031:Some of the configuration assistants failed/cancelled. 这是安装过程中常见的错误之一. 引起此错误的原因:/etc/hosts ...

  4. liunx命令之whereis、which、find的区别和联系

    liunx命令之whereis.which.find的区别和联系

  5. Linux下使用GDB调试程序

    问题描述:          Linux下使用GDB调试程序 问题解决:          (1)生成调试文件 注:         使用命令   gdb IOStream.c   -o IOStre ...

  6. TDD 实践过程

    聚合根是唯一能够允许充当进入聚合的逻辑途径的实体. 是唯一能够允许聚合外的对象持有的引用成员. 聚合根--决定数据库的入口,以为:

  7. 批量扫描互联网无线路由设备telnet,并获取WIFI密码

    批量扫描互联网无线路由设备telnet,并获取WIFI密码 http://lcx.cc/?i=4513

  8. Linux---弹球游戏

    ReadMe: 弹球游戏按键说明(注意大小写): Q End Up Game 游戏停止 P Play Again  再玩一次 f 速度x轴方向减速 s 速度x轴方向加速 F 速度y轴方向减速 S 速度 ...

  9. 2.Adding a Controller

    MVC stands for model-view-controller.  MVC is a pattern for developing applications that are well ar ...

  10. 版本管理工具介绍—Git篇

    前篇 如题,提起版本管理工具相信做C#开发 还是对Git比较陌生  我们可能更熟悉vss.svn 记录此文的目的 更是为以后的前段学习做基础  现在的技术比如nodeJs  angularJs ==都 ...