【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 ...
随机推荐
- Win10:如何修改双网卡的优先级?
很多使用双网卡的IT之家网友可能遇到一种情况,比如笔记本电脑在插上网线后还是用WiFi,得手动关闭无线连接才能转换到有线连接.如何才能调整合适的网络优先级呢?一般来说,有两种方法比较常用. 一.调整网 ...
- dropdown多选下拉框
写好了一个dropdown多选框.直接粘下面代码就能用 效果展示: temp2.jsp <%@page import="com.util.LabelCacheManager" ...
- python之filter()函数
filter()函数是python内置的一个高阶函数. filter()函数接受一个函数f 和一个list,这个函数f的作用是对每个元素进行判断,返回True或False,filter()根据判断结果 ...
- Cut the rope
http://acm.nyist.net/JudgeOnline/problem.php?pid=651 描述We have a rope whose length is L. We will cut ...
- zmq重点
The zmq_msg_send(3) method does not actually send the message to the socket connection(s). It queues ...
- Python_selenium之执行JavaScript
Python_selenium之执行JavaScript 一.简略的介绍selenium执行JavaScript 1. Example 1进入浏览器之后,弹出一个alert弹框 #coding:utf ...
- MySQL的limit子句
1.理解: limit用来取结果集中的固定几条记录 2.参数: limit offset,pagesize offset:偏移量,为0时,可以省略 pagesize:每页显示的行数,通常是固定的 0表 ...
- go语言发送邮件
package main import ( "fmt" "net/smtp" "strings" ) //发送邮件的逻辑函数 func Se ...
- js 日期加一天
经常在js 重要做时间加一的处理记录一下 ps:时间格式为:'2017-03-30' 一:源码: //时间加一天 function addDate(date, days) { if (days == ...
- Xamarin.Forms学习之XAML命名空间
大家好,我又悄咪咪的来了,在上一篇的Xamarin文章中简单介绍了Xamarin的安装过程,妈蛋没想到很多小朋友很感激我,让他们成功的安装了Xamarin,然后......成功的显示了经典的两个单词( ...