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

题解:又见递归的解法。这道题交了好多次,主要是细节要想清楚。总结一下要注意的地方:

  • s为0的时候,如果p为1,false;否则如果p的第1位上为'*',那么就要考察p后面的元素,于是递归调用 isMatch(s, p.substring(2)) ,这样的例子有s = "", p = "c*c*";如果p的第一位上不为1,那么s和p肯定不匹配了。
  • 当p长度为1的时候要单独处理,因为这时候我们不用判断p的第1位是否是‘*’了。处理这一段代码如下:
  1. if(p.length() == 1){
    if(p.charAt(0) == '.' && s.length() == 1)
    return true;
    return s.equals(p);
    }
  • 其他情况就要按照p的第1位是否为'*‘来分了。如果不为’*‘,那么p和s的第0位必须匹配(相等或p第0位为'.'),否则p和s不匹配,这样的例子类似的有s = "ab", p = "c*b"。如果为'*',我们就按照匹配0位,1位,2位.....的方式递归试探,类似的例子有s = "abbc", p = "ab*bbc",此时'*'并不匹配s中的任何字符,再有s = "aa",p = "a*",此时'*'匹配s中的两个a。

代码如下:

 public class Solution {
public boolean isMatch(String s, String p) {
if(p.length() == 0)
return s.length() == 0; if(s.length() == 0){
if(p.length() == 1)
return false;
if(p.charAt(1) == '*')
return isMatch(s, p.substring(2));
return false;
} if(p.length() == 1){
if(p.charAt(0) == '.' && s.length() == 1)
return true;
return s.equals(p);
} if(p.length() >= 2 && p.charAt(1) != '*'){
//if p(1) is not *, we need p(0) equals to s(0) or p(0) equals '.'
if(p.charAt(0) == s.charAt(0) || p.charAt(0) == '.' && s.length() != 0)
//check if the left is also match
return isMatch(s.substring(1), p.substring(1));
return false;
}
else{
//if p(1) is '*',we check how many we can match from this * by trying
int i = 0;
char now = p.charAt(0);
while(i<s.length() && (s.charAt(i) == now || now == '.')){
if(isMatch(s.substring(i+1),p.substring(2)))
return true;
i++;
}
//this means we don't use this '*' to match any character, just skip it
return isMatch(s, p.substring(2));
}
}
}

【leetcode刷题笔记】Regular Expression Matching的更多相关文章

  1. 刷题10. Regular Expression Matching

    一.题目说明 这个题目是10. Regular Expression Matching,乍一看不是很难. 但我实现提交后,总是报错.不得已查看了答案. 二.我的做法 我的实现,最大的问题在于对.*的处 ...

  2. 【leetcode刷题笔记】Wildcard Matching

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  3. LeetCode(10) Regular Expression Matching

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

  4. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  5. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  6. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  7. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

  8. LeetCode刷题笔记(1-9)

    LeetCode1-9 本文更多是作为一个习题笔记,没有太多讲解 1.两数之和 题目请点击链接 ↑ 最先想到暴力解法,直接双循环,但是这样复杂度为n平方 public int[] twoSum(int ...

  9. leetcode刷题笔记

    (1)Best Time to Buy and Sell Stock Total Accepted: 10430 Total Submissions: 33800My Submissions Say ...

随机推荐

  1. Thinkphp5 使用命令行模式(cli模式)

    Tp5的cli模式跟Tp3.2变化较大,有自己的一套方式,在这里做个搬运工,把Tp文档的东西搬运过来,方便大家. 原出处截图 创建自定义命令行 第一步,配置command.php文件,目录在appli ...

  2. YII安装步骤(windows)

    一.首先你得下个YII框架的源码 :下载地址:http://www.yiiframework.com/download/ 二.把下载到的源码解压放到一个PHP可访问的位置:如我的 F:\site(已具 ...

  3. servelet 直接输出内容

    package helloworld; import java.io.IOException; import javax.servlet.ServletException; import javax. ...

  4. 第一百六十八节,jQuery,表单选择器

    jQuery,表单选择器 学习要点: 1.常规选择器 2.表单选择器 3.表单过滤器 表单作为 HTML 中一种特殊的元素,操作方法较为多样性和特殊性,开发者不但可以 使用之前的常规选择器或过滤器,也 ...

  5. 微信 oauth 授权3

    3. 请求 2的url会得到 { "access_token": "OezXcEiiBSKSxW0eoylIeAsR0GmYd1awCffdHgb4fhS_KKf2Cot ...

  6. Scrapy爬虫笔记

    Scrapy是一个优秀的Python爬虫框架,可以很方便的爬取web站点的信息供我们分析和挖掘,在这记录下最近使用的一些心得. 1.安装 通过pip或者easy_install安装: 1 sudo p ...

  7. docker搭建lnmp环境(问题,资料,命令)

    入门参考 http://www.runoob.com/docker/docker-install-nginx.html 十大常用命令玩转docker 1. #从官网拉取镜像 docker pull & ...

  8. Log4j 使用

    源博客 http://www.cnblogs.com/alipayhutu/archive/2012/06/21/2558249.html#3159794 [1]从零开始 a). 新建Java Pro ...

  9. 超哥mysql数据库部分blog整理:

    总目录:mysql数据库阶段学习目录 https://www.cnblogs.com/clschao/articles/10065275.html Day1. 1.数据库初识 https://www. ...

  10. 理解CSS3属性transition

    一.说明 1.1 定义和用法 transition 属性是一个简写属性,用于设置四个过渡属性: transition-property:规定设置过渡效果的CSS属性的名称. transition-du ...