10. Regular Expression Matching

  • Total Accepted: 89193
  • Total Submissions: 395441
  • Difficulty: Hard

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://www.tuicool.com/articles/Ebiymu
  • 首先要理解题意:

    • "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()
 public boolean isMatch(String s, String p) {
// base case
if (p.length() == 0) {
return s.length() == 0;
} // special case
if (p.length() == 1) {
// if the length of s is 0, return false
if (s.length() < 1) {
return false;
}
//if the first does not match, return false
else if ((p.charAt(0) != s.charAt(0)) && (p.charAt(0) != '.')) {
return false;
} // otherwise, compare the rest of the string of s and p.
else {
return isMatch(s.substring(1), p.substring(1));
}
} // case 1: when the second char of p is not '*'
if (p.charAt(1) != '*') {
if (s.length() < 1) {
return false;
}
if ((p.charAt(0) != s.charAt(0)) && (p.charAt(0) != '.')) {
return false;
} else {
return isMatch(s.substring(1), p.substring(1));
}
} // case 2: when the second char of p is '*', complex case.
else {
//case 2.1: a char & '*' can stand for 0 element
if (isMatch(s, p.substring(2))) {
return true;
} //case 2.2: a char & '*' can stand for 1 or more preceding element,
//so try every sub string
int i = 0;
while (i<s.length() && (s.charAt(i)==p.charAt(0) || p.charAt(0)=='.')){
if (isMatch(s.substring(i + 1), p.substring(2))) {
return true;
}
i++;
}
return false;
}
}
  

No.010 Regular Expression Matching的更多相关文章

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

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

  2. LeetCode--No.010 Regular Expression Matching

    10. Regular Expression Matching Total Accepted: 89193 Total Submissions: 395441 Difficulty: Hard Imp ...

  3. 【LeetCode】010. Regular Expression Matching

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

  4. 010 Regular Expression Matching 正则表达式匹配

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

  5. LeetCode 010 Regular Expression Matching

    题目描述:Regular Expression Matching Implement regular expression matching with support for '.' and '*' ...

  6. 《LeetBook》leetcode题解(10): Regular Expression Matching——DP解决正则匹配

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  7. No.010:Regular Expression Matching

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

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

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

  9. [LeetCode] 10. Regular Expression Matching

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

随机推荐

  1. CXF发布restful WebService的入门例子(服务器端)

    研究了两天CXF对restful的支持.   现在,想实现一个以 http://localhost:9999/roomservice 为入口, http://localhost:9999/roomse ...

  2. Objective-C中nil与release的区别与用法

    首先说一下他们两的作用,nil就是把一个对象的指针置为空,只是切断了指针与内存中对象的联系,它对内存的释放没有什么作用:而release才是真正用于内存释放的,release后系统会将该块内存标记为可 ...

  3. [CSS]置换和非置换元素

    转自:http://blog.doyoe.com 先进一个题外话 在面试一个 重构(各大公司的叫法可能不太一样)时,我喜欢从一个点开始问,然后一直延展下去成为一条线,甚至是一个面,直到问到不会的地方, ...

  4. 在windows下添加php的Imagick扩展

    安装ImageMagick-6.9.2-6-Q16-x64-dll.exe 将安装目录下的CORE_开头的dll文件和X11.dll文件复制到c:\windows\system32\下, 在windo ...

  5. Android listview中使用checkbox

    最近比较忙碌,我也不知道忙的什么东西,打算写的博客写了一半,还没写完,今天先扯一扯项目中遇到的一个问题,一方面防止以后遇到这个问题忘记如何解决,另一方面希望可以提供给遇到同样问题的朋友一个思路.下面开 ...

  6. 转--2014年最新810多套android源码2.46GB免费一次性打包下载

    转载自:http://www.eoeandroid.com/thread-497046-1-1.html 感谢该博客主人无私奉献~~ 下面的源码是从今年3月份开始不断整理源码区和其他网站上的安卓例子源 ...

  7. Mac下java编译乱码(适用于maven , ant)

    将~/.bash_profile中添加如下即可 export LC_ALL=en

  8. 项目中用到的logback列子

    <?xml version="1.0" encoding="UTF-8"?> <configuration> <appender ...

  9. rbenv安装ruby2.3.0在线安装不上。老子出绝招了(更新)

    今天把系统换成Linux mint了.感觉比ubuntu的好用太多,细节真是不错,Ubuntu感觉就是毛坯房,Linux mint真是精装修啊 问题来了.安装rbenv后,然后安装rbenv-buil ...

  10. 标准化命名CSS类,持续更新

    放链接.持续化更新,以后可能会用上.https://github.com/zhangxinxu/zxx.lib.css/blob/master/zxx.lib.css