LeetCode 10 Regular Expression Matching(字符串匹配)
'.' Matches any single character.匹配任何单字符
'*' Matches zero or more of the preceding element.匹配0个或者多个前置元素
采用动态规划方法
public boolean isMatch(String s, String p)
1, If p.charAt(j) == s.charAt(i) : dp[i][j] = dp[i-1][j-1];
进行下一层的计算
2, If p.charAt(j) == '.' : dp[i][j] = dp[i-1][j-1];
字符为’.'时也进行下一层dp[i-1][i-1]运算
3, If p.charAt(j) == '*’:
当字符为’*’时,需要进行分类考虑
here are two sub conditions:
1 if p.charAt(j-1) != s.charAt(i) : dp[i][j] = dp[i][j-2] //in this case, a* only counts as empty
当’*’前面的一个字符和匹配串字符不相同时,则从模式串删去*以及其前面一个字符
2 if p.charAt(i-1) == s.charAt(i) or p.charAt(i-1) == '.’:
dp[i][j] = dp[i-1][j] //in this case, a* counts as multiple a
or dp[i][j] = dp[i][j-1] // in this case, a* counts as single a
or dp[i][j] = dp[i][j-2] // in this case, a* counts as empty
参考代码:
package leetcode; /***
*
* @author pengfei_zheng
* 字符串匹配问题
*/
public class Solution10{
public boolean isMatch(String s, String p) {
return match(s,p,0,0);
}
private boolean match(String s,String p,int i,int j){//其中i,j分别为开始下标
if(j==p.length())//匹配至长度相同
return i==s.length();
if(j==p.length()-1 || p.charAt(j+1)!='*'){//匹配至下一个字符不为'*'
if(i==s.length() || s.charAt(i)!=p.charAt(j) && p.charAt(j)!='.')//不相等或者不等于'.'
return false;
else
return match(s,p,i+1,j+1);
}
while(i<s.length() && (s.charAt(i)==p.charAt(j) || p.charAt(j)=='.')){//相等或者等于'.'
if(match(s,p,i,j+2))
return true;
i++;
}
return match(s,p,i,j+2);
}
}
LeetCode 10 Regular Expression Matching(字符串匹配)的更多相关文章
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- Leetcode 10. Regular Expression Matching(递归,dp)
10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...
- leetcode 10. Regular Expression Matching 、44. Wildcard Matching
10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...
- [LeetCode] 10. Regular Expression Matching 正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- LeetCode (10): Regular Expression Matching [HARD]
https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...
- [LeetCode]10. Regular Expression Matching正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- [leetcode]10. Regular Expression Matching正则表达式的匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- [LeetCode] 10. Regular Expression Matching ☆☆☆☆☆
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- [LeetCode] 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...
- Java [leetcode 10] Regular Expression Matching
问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
随机推荐
- Linux基础网络设置
查看Linux网络参数 ifconfig—-查看网络接口 > [root@localhost ~]# ifconfig eth1 Link encap:Ethernet HWaddr 00:0C ...
- 刚看完了一本关于javascript的书感觉受益匪浅,原来不懂的东西这么多,想问问怎么成为大神?求教!!!!!!
刚看完了一本关于javascript的书感觉受益匪浅,原来不懂的东西这么多,想问问怎么成为大神?求教!!!!!!
- ASP.NET MVC Castle Windsor 教程
一.[转]ASP.NET MVC中使用Castle Windsor 二.[转]Castle Windsor之组件注册 平常用Inject比较多,今天接触到了Castle Windsor.本篇就来体验其 ...
- SOAP消息头的处理
SOAP消息头的处理 WebService学习总结(四)——调用第三方提供的webService服务 SOAP中 RPC/ENC 为啥被抛弃
- spring 配置 Java配置类装配bean
https://www.cnblogs.com/chenbenbuyi/p/8457700.html 自动化装配的确有很大的便利性,但是却并不能适用在所有的应用场景,比如需要装配的组件类不是由自己的应 ...
- SQLSERVER 2008 技术内幕 T-SQL查询 笔记1: SQL 执行顺序
与大多数语言一样,SQL语言也有一个执行顺序,只是在大多数编程语言中,代码是按照编写顺序来处理的,而在SQL中则不是,下图为SQL 执行顺序. () ) [ ALL | DISTINCT ] () [ ...
- 软件设计模式之工厂模式(JAVA)
什么是工厂模式? 工厂模式是我们最常用的实例化对象模式了,是用工厂方法代替new操作的一种模式.著名的Jive论坛 ,就大量使用了工厂模式,工厂模式在Java程序系统可以说是随处可见.因为工厂模式就相 ...
- Java初学者不可不知的MyEclipse的设置技巧(自动联想功能)
最近初学Java,正在使用MyEclipse来编写新的项目,刚开始打开MyEclipse感觉这个工具既陌生又熟悉,熟悉之处在于编辑器的几大共通之处它都具备,比如说基本的设置.编辑区.调试区都是类似的, ...
- Android四大组件之——ContentProvider(二)
Content Resolver介绍: 开发者文档中这么定义的: This class provides applications access to the content model. 这个类为应 ...
- SSM是什么框架?
SSM框架,是Spring + Spring MVC + MyBatis的缩写,这个是继SSH之后,目前比较主流的Java EE企业级框架,适用于搭建各种大型的企业级应用系统. 1.Spring简介 ...