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. jQuery CSS 操作函数

    CSS 属性 描述 css() 设置或返回匹配元素的样式属性. height() 设置或返回匹配元素的高度. offset() 返回第一个匹配元素相对于文档的位置. offsetParent() 返回 ...

  2. python __new__ __init__ __del__

    1.python实例化顺序是.__new__ -->__init__ --> __del__ 2.如果重写new没return,就实例化不成功

  3. 目前国际上所用云计算平台IaaS、PaaS、SaaS简介

    随着云计算这个概念越来越为人所熟知,企业对云计算的重视程度也在日趋加深.这不仅是一种潮流,更体现了一种需求——数字化.现代化.科技化的整体需求.如今市场上云计算的运营商更是风起云涌,服务种类更是丰富繁 ...

  4. Linux 并发服务器雏形总结

    如下介绍一个并发回射客户端/服务器的雏形,所谓回射:就是客户端输入一条数据,服务器端读取并显示,然后服务器端再把刚读取的信息发送回客户端进行显示.示意图如下: 所谓并发服务器:就是一个服务器可以同时为 ...

  5. 【BZOJ4821】[Sdoi2017]相关分析 线段树

    [BZOJ4821][Sdoi2017]相关分析 Description Frank对天文学非常感兴趣,他经常用望远镜看星星,同时记录下它们的信息,比如亮度.颜色等等,进而估算出星星的距离,半径等等. ...

  6. Ants(二分图最佳完美匹配)

    Ants Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6904   Accepted: 2164   Special Ju ...

  7. CAS单点登录------未认证授权服务

    问题背景:之前我使用的127.0.0.1进行CAS 直接url 进行过滤! 后来我用nginx 进行反向代理 出现问题:  如下图 第一眼,就在内心想,草这什么鬼! 麻蛋!     ON! 调试了五分 ...

  8. make tree install 目录树状结构工具安装

    http://futeng.iteye.com/blog/2071867 http://zhou123.blog.51cto.com/4355617/1196415 wget ftp://mama.i ...

  9. VS中没有为此解决方案配置选中要生成的项目

    菜单->生成->配置管理器->给要生成的项目打钩

  10. RecyclerView添加分割线

    mRecyclerView = findView(R.id.id_recyclerview); //设置布局管理器 mRecyclerView.setLayoutManager(layout); // ...