【leetcode刷题笔记】Regular Expression Matching
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位是否是‘*’了。处理这一段代码如下:
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的更多相关文章
- 刷题10. Regular Expression Matching
一.题目说明 这个题目是10. Regular Expression Matching,乍一看不是很难. 但我实现提交后,总是报错.不得已查看了答案. 二.我的做法 我的实现,最大的问题在于对.*的处 ...
- 【leetcode刷题笔记】Wildcard Matching
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- LeetCode(10) Regular Expression Matching
题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single charac ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- LeetCode刷题笔记(1-9)
LeetCode1-9 本文更多是作为一个习题笔记,没有太多讲解 1.两数之和 题目请点击链接 ↑ 最先想到暴力解法,直接双循环,但是这样复杂度为n平方 public int[] twoSum(int ...
- leetcode刷题笔记
(1)Best Time to Buy and Sell Stock Total Accepted: 10430 Total Submissions: 33800My Submissions Say ...
随机推荐
- Groovy基本类型与运算符
字符串 1.1字符串段落 def s = """Groovy Grails JAVA """ 输出结果: Groovy Grails JAV ...
- 基于consul构建golang系统分布式服务发现机制
原文地址-石匠的Blog: http://www.bugclosed.com/post/5 在分布式架构中,服务治理是一个重要的问题.在没有服务治理的分布式集群中,各个服务之间通过手工或者配置的方式进 ...
- 基于docker的webrtc开发环境
在root目录下先放好自定义的constants.py文件再docker run 同时记得修改PUBLIC_IP 可以跨wifi通信 docker pull piasy/apprtc-server d ...
- plsql programming 12 集合(忽略, 个人感觉用不到)
关联数组, 嵌套表, varray 个人并不推荐使用集合, 因为操作有别于普通字段. 集合中每一个元素的数据类型都是相同的, 因此这些元素都是同质的(同质元素) 这一章的内容先忽略吧, 因为个人感觉用 ...
- Java基础11 对象引用(转载)
对象引用 我们沿用之前定义的Human类,并有一个Test类: public class Test{ public static void main(String[] args){ ...
- Servlet 服务器 HTTP 响应
状态行包括 HTTP 版本(在本例中为 HTTP/1.1).一个状态码(在本例中为 200)和一个对应于状态码的短消息(在本例中为 OK). 下表总结了从 Web 服务器端返回到浏览器的最有用的 HT ...
- CentOS 6.5 Git源码安装
首先清除系统自带git,使用如下命令 yum -y remove git 一.下载Git源码包 wget https://www.kernel.org/pub/software/scm/git/git ...
- Unity与Android交互实现
主要参考了这篇文章: Unity与Android交互方案优化版 链接:https://www.jianshu.com/p/86b275da600e 自己的实现(unity获取内存和温度): andro ...
- Python gevent学习笔记
gevent是Python的一个用于网络IO的函数库,其中应用到了 coroutine(协同程序) 的思想.首先来了解下目前网络框架的几种基本的网络I/O模型: 阻塞式单线程:这是最基本的I/O模型, ...
- 如何学习TP框架
1.学习访问方法 2.控制器的写法 3.视图的写法 4.模型的写法 5.扩展类的用法 6.扩展插件的用法