【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 ...
随机推荐
- 如何使用 TP中的公共函数 (定义在common/common.php中的函数)
如何使用 TP中的公共函数 (定义在common/common.php中的函数) (2011-09-30 15:32:09) 转载▼ 标签: 杂谈 1.在common/common.php 中有个 ...
- 【转】Monkey测试2——Monkey测试策略
Monkey的测试策略 一. 分类 Monkey测试针对不同的对象和不同的目的采用不同的测试方案,首先测试的对象.目的及类型如下: 测试的类型分为:应用程序的稳定性测试和压力测试 测试对象分为:单一a ...
- Windows之建立C++开发环境
下载:https://yun.baidu.com/s/1pK7j4Fp 解压得到 把myMingw文件夹复制系统根目录下. 添加C:\myMingw\bin到系统环境变量 双击make-3.81.ex ...
- Web services 把 Web 应用程序提升到了另外一个层面
通过使用 Web services,您的应用程序可向全世界发布功能或消息. Web services 使用 XML 来编解码数据,并使用 SOAP 借由开放的协议来传输数据. 通过 Web servi ...
- Spring MVC密码处理
以下示例显示如何在使用Spring Web MVC框架的表单中使用密码.首先使用Eclipse IDE来创建一个WEB工程,并按照以下步骤使用Spring Web Framework开发基于动态表单的 ...
- Prime pair connection (Project Euler 134)
题目大意: 对于连续的质数$p1$, $p2$, 满足$5 <= p1 <= 1000000$ 求出最小的整数$S$, 它以 $p1$结尾并且能够被$p2$整除. 求$S$的和. 思路: ...
- DBCP与C3P0数据库连接池
数据库连接池是做什么的? 学过计算机网络的都知道,在一个内部局域网中.大部分用的都是私有地址,要想和外部 打交道,必须要有相应的合法外部地址相相应.然而内部用户数量巨大.一台机子一个外部IP 是不现实 ...
- 微信 oauth 授权3
3. 请求 2的url会得到 { "access_token": "OezXcEiiBSKSxW0eoylIeAsR0GmYd1awCffdHgb4fhS_KKf2Cot ...
- 图像sift配准后融合
image rectification 图像校正 在配准时,先找到特征点,找到特征点后剔除伪匹配点. 然后针对两幅图像做几何矫正(一般通过估计出来的仿射矩阵完成). 这部完成后,图像可以匹配了,但是两 ...
- Python gevent学习笔记
gevent是Python的一个用于网络IO的函数库,其中应用到了 coroutine(协同程序) 的思想.首先来了解下目前网络框架的几种基本的网络I/O模型: 阻塞式单线程:这是最基本的I/O模型, ...