问题描述:

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

  1. '.' Matches any single character.
  2. '*' Matches zero or more of the preceding element.
  3. http://i.cnblogs.com/EditPosts.aspx?opt=1
  4. The matching should cover the entire input string (not partial).
  5.  
  6. The function prototype should be:
  7. bool isMatch(const char *s, const char *p)
  8.  
  9. Some examples:
  10. isMatch("aa","a") false
  11. isMatch("aa","aa") true
  12. isMatch("aaa","aa") false
  13. isMatch("aa", "a*") true
  14. isMatch("aa", ".*") true
  15. isMatch("ab", ".*") true
  16. 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的位置从现在开始进行暴力比较,每次向后移一位进行递归比较。

代码如下:

  1. public class Solution {
  2. public static boolean isMatch(String s, String p) {
  3. if (s == null)
  4. return p == null;
  5. if (p == null)
  6. return s == null;
  7. return helper(s, p, 0, 0);
  8. }
  9. private static boolean helper(String s, String p, int sp, int pp) {
  10. if (pp >= p.length())
  11. return sp >= s.length();
  12. // pp comes to end
  13. if (pp == p.length() - 1) {
  14. if (sp >= s.length()
  15. || (s.charAt(sp) != p.charAt(pp) && p.charAt(pp) != '.'))
  16. return false;
  17. else
  18. return helper(s, p, sp + 1, pp + 1);
  19. }
  20. // p.charAt(pp+1)!='*'
  21. if (p.charAt(pp + 1) != '*') {
  22. if (sp >= s.length()
  23. || (s.charAt(sp) != p.charAt(pp) && p.charAt(pp) != '.'))
  24. return false;
  25. else
  26. return helper(s, p, sp + 1, pp + 1);
  27. }
  28. // p.charAt(pp+1)=='*'
  29. while (sp < s.length()
  30. && (p.charAt(pp) == '.' || s.charAt(sp) == p.charAt(pp))) {
  31. if (helper(s, p, sp, pp + 2))
  32. return true;
  33. sp++;
  34. }
  35. return helper(s, p, sp, pp + 2);
  36. }
  37. }

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. jQuery 的 json 格式的处理问题

    在实际的使用中时常会发生一些ajax验证的诡异事件,如我在一个文件中使用json传送数据,结果出现了当数据发送到服务器端时(后端主要呈现的是对json数据对象的数据库查询操作),结果显示的是数据已经插 ...

  2. Linux内核中的常用宏container_of

    Container_of在Linux内核中是一个常用的宏,用于从包含在某个结构中的指针获得结构本身的指针,通俗地讲就是通过结构体变量中某个成员的首地址进而获得整个结构体变量的首地址. Containe ...

  3. Unity3d 如何找到游戏对象并改变其颜色

    //游戏对象 private var obj:GameObject; //渲染器 private var render:Renderer; //贴图 private var texture:Textu ...

  4. EntityFramework.Extended

    记录 Entity Framework扩展,可以实现批量更新.删除,但需要EntityFramework6.0支持,需要支持低版本的EF,可下载该扩展的低版本. https://www.nuget.o ...

  5. Transaction Log Truncation

    --method 1-- ALTER DATABASE KIS_Sample3 SET RECOVERY SIMPLE ) ALTER DATABASE KIS_Sample3 SET RECOVER ...

  6. 去掉url后面的#

    <a href="#" 这个代码,当你点击后会在url后面添加# 怎么去掉呢?解决方法:点击这里 <a href="javascript:void(0)&qu ...

  7. mysql 增加删除用户

    mysql 增加用户 (注意:因为MYSQL环境中的命令,所以后面都带一个分号作为命令结束符) 格式:grant select on 数据库.* to 用户名@登录主机 identified by ' ...

  8. MVC中的@Html.DisplayFor等方法如何控制日期的显示格式(转) - Rising

    在Sql Server2005中,如果将某字段定义成日期  时间   类型DateTime,那么在视图中会默认显示成年月日时分秒的方式(如    2013/8/6 13:37:33) 如果只想显示成年 ...

  9. Unity3D开发(一):NGUI之UIRoot屏幕分辨率自适应

    原地址:http://blog.csdn.net/onerain88/article/details/11713299 NGUI在Unity3D游戏开发中非常常用,而NGUI对于每一个UI场景,都是以 ...

  10. APP 上传之后出现"invalid binary" 问题解决汇总

    背景 5.1 号开始 App 审核开始强制支持 iPhone5,并禁止使用 UDID. 问题 上传 app 后一直处于 Invalid Binary 状态,并且收到一封邮件说 Non-public A ...