题目

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

'.' Matches any single character.
'*' Matches zero or more of the preceding element. 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 题解:
本文及代码引用自:http://harrifeng.github.io/algo/leetcode/regular-expression-matching.html
  • 首先要理解题意:

    • "a"对应"a", 这种匹配不解释了
    • 任意字母对应".", 这也是正则常见
    • 0到多个相同字符x,对应"x*", 比起普通正则,这个地方多出来一个前缀x. x代表的是
      相同的字符中取一个,比如"aaaab"对应是"a*b"
    • "*"还有一个易于疏忽的地方就是它的"贪婪性"要有一个限度.比如"aaa"对应"a*a",
      代码逻辑不能一路贪婪到底
  • 正则表达式如果期望着一个字符一个字符的匹配,是非常不现实的.而"匹配"这个问题,非
    常容易转换成"匹配了一部分",整个匹配不匹配,要看"剩下的匹配"情况.这就很好的把
    一个大的问题转换成了规模较小的问题:递归
  • 确定了递归以后,使用java来实现这个问题,会遇到很多和c不一样的地方,因为java对字符
    的控制不像c语言指针那么灵活charAt一定要确定某个位置存在才可以使用.
  • 如果pattern是"x*"类型的话,那么pattern每次要两个两个的减少.否则,就是一个一个
    的减少. 无论怎样减少,都要保证pattern有那么多个.比如s.substring(n), 其中n
    最大也就是s.length()
代码如下:
 1     public static boolean isMatch(String s, String p) {
 2         if (p.length() == 0)
 3             return s.length() == 0;
 4 
 5         // length == 1 is the case that is easy to forget.
 6         // as p is subtracted 2 each time, so if original
 7         // p is odd, then finally it will face the length 1
 8         if (p.length() == 1)
 9             return (s.length() == 1) && (p.charAt(0) == s.charAt(0) || p.charAt(0) == '.');
 
         // next char is not '*': must match current character
         if (p.charAt(1) != '*') {
             if (s.length() == 0)
                 return false;
             else
                 return (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.')
                         && isMatch(s.substring(1), p.substring(1));
         }else{
             // next char is *
             while (s.length() > 0 && (p.charAt(0) == s.charAt(0) || p.charAt(0) == '.')) {
                 if (isMatch(s, p.substring(2))) 
                     return true;
                 s = s.substring(1);
             }
             return isMatch(s, p.substring(2));
         }
     }
 

Regular Expression Matching leetcode java的更多相关文章

  1. Regular Expression Matching leetcode

    递归方法运行时间过长.考虑使用动态规划的方法. 代码如下: bool isMatch(string s, string p) { int i,j; int m=s.size(); int n=p.si ...

  2. lc面试准备:Regular Expression Matching

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

  3. 【JAVA、C++】LeetCode 010 Regular Expression Matching

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

  4. Java [leetcode 10] Regular Expression Matching

    问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...

  5. LeetCode第[10]题(Java):Regular Expression Matching

    题目:匹配正则表达式 题目难度:hard 题目内容:Implement regular expression matching with support for '.' and '*'. '.' Ma ...

  6. [leetcode]Regular Expression Matching @ Python

    原题地址:https://oj.leetcode.com/problems/regular-expression-matching/ 题意: Implement regular expression ...

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

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

  8. LeetCode | Regular Expression Matching

    Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...

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

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

随机推荐

  1. HDU5320 : Fan Li

    考虑枚举左端点i,则随着右端点的右移,一共只有$O(\log n)$种不同的gcd取值.所以首先通过ST表+二分查找预处理出$O(n\log n)$个四元组(x,i,l,r),表示左端点为i,右端点取 ...

  2. jquery开发表格插件项目之知识点累积 二

    $.addClass() 增加样式 $.removeClass() 去除样式 $.toggleClass() 单击增加样式,再单击取消样式 $.hasClass() 判断是否存在样式,存在返回true ...

  3. Gson通过借助TypeToken获取泛型参数的类型的方法(转)

    最近在使用Google的Gson包进行Json和Java对象之间的转化,对于包含泛型的类的序列化和反序列化Gson也提供了很好的支持,感觉有点意思,就花时间研究了一下. 由于Java泛型的实现机制,使 ...

  4. 在Ubuntu的系统中怎样将应用程序加入到開始菜单中

    /*********************************************************************  * Author  : Samson  * Date   ...

  5. 从零开始部署CAS服务器

    从0开始部署CAS服务器的操作过程文档,我已经整理完毕,一共分为8步,这8步都是我自己操作实践过的. Setp1:Ubuntu server安装 在virtual box中安装ubuntu serve ...

  6. 《Go语言实战》摘录:7.1 并发模式 - runner

    7.1 并发模式 - runner

  7. discuz修改太阳,月亮,星星等级图标

    想必大家都想修改一下默认的等级图标吧,刚才在论坛上看见很多大神的方法都是要修改文件的,不过为了安全起见需要事先备份好才改,这种方法是可行的,但可能有些新手站长不会修改,又或者改错了恢复不来,现在我教大 ...

  8. redis主从集群搭建及容灾部署(哨兵sentinel)

    Redis也用了一段时间了,记录一下相关集群搭建及配置详解,方便后续使用查阅. 提纲 Redis安装 整体架构 Redis主从结构搭建 Redis容灾部署(哨兵sentinel) Redis常见问题 ...

  9. 依赖注入(DI)和控制反转(IOC)的理解,写的太好了。

    学习过spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...

  10. WinForm通用自动更新器AutoUpdater项目实战

    一.项目背景介绍 最近单位开发一个项目,其中需要用到自动升级功能.因为自动升级是一个比较常用的功能,可能会在很多程序中用到,于是,我就想写一个自动升级的组件,在应用程序中,只需要引用这个自动升级组件, ...