【leetcode】10.Regular Expression Matching
题目描述:
Implement regular expression matching with support for '.'
and '*'
.
'.' Matches any single character.
'*' Matches zero or more of the preceding element.
解题思路:
这道题如果只考虑“.”的话其实很好完成,所以解题的关键在于处理“*”的情况。以为“*”与前一个字母有关,所以应该整体考虑ch*……的情况。ch*可以匹配0-n个s的字符串,(n是s的起始位置开始值为ch的字符的个数)。当然还要考虑.*……的情况,这样的情况系,就要考虑把.*与s的所有字符都匹配一遍,看能不能找出结果。其他的考虑情况比较容易想到,看下面的代码即可。
具体代码:
public static boolean isMatch(String s, String p) {
//p为null或者长度为0的情况
if(p==null){
return s==null;
}
if(p.length()==0){
return s.length()==0;
}
if(p.length()==1){
if(s.length()!=1){
return false;
}
else{
if(p.charAt(0)=='.'){
return true;
}
else if(p.charAt(0)=='*'){
return false;
}
else{
return p.charAt(0)==s.charAt(0);
}
}
}
//p至少有长度为2
if(p.contains("*")|| p.contains(".")){
//ch*情况的处理
if(p.charAt(1)=='*'){
char ch = p.charAt(0);
//.*的情况,.*可以匹配s的任意个字符,所以把每种可能的情况递归一遍
if(ch=='.'){
for(int i=0;i<=s.length();i++){
boolean key = isMatch(s.substring(i), p.substring(2));
if(key==true)
return true;
}
}
//ch*的情况,ch*可以匹配0-n个s的字符串,(n是s的起始位置开始值为ch的字符的个数)
else{
int index=0;
while(index<s.length() && s.charAt(index)==p.charAt(0)){
index++;
}
for(int i=0;i<=index;i++){
boolean key = isMatch(s.substring(i), p.substring(2));
if(key==true)
return true;
}
}
}
//不是ch*的情况,即chch……的情况,这时候s的长度要保证大于0
else{
if(p.charAt(0)=='.'){
if(s.length()==0){
return false;
}
boolean key = isMatch(s.substring(1), p.substring(1));
return key;
}
else{
if(s.length()==0){
return false;
}
//如果开头字符相等,匹配与否取决于两字符串除去第一个字符后是否匹配
if(p.charAt(0)==s.charAt(0)){
boolean key = isMatch(s.substring(1), p.substring(1));
return key;
}
else{
return false;
}
}
} return false;
}
//p不包含*,.的情况
else{
if(s.equals(p))
return true;
return false;
}
}
【leetcode】10.Regular Expression Matching的更多相关文章
- 【LeetCode】10.Regular Expression Matching(dp)
[题意] 给两个字符串s和p,判断s是否能用p进行匹配. [题解] dp[i][j]表示s的前i个是否能被p的前j个匹配. 首先可以分成3大类情况,我们先从简单的看起: (1)s[i - 1] = p ...
- 【一天一道LeetCode】#10. Regular Expression Matching
一天一道LeetCode系列 (一)题目 Implement regular expression matching with support for '.' and '*'. '.' Matches ...
- 【LeetCode】010. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- leetcode problem 10 Regular Expression Matching(动态规划)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 《LeetBook》leetcode题解(10): Regular Expression Matching——DP解决正则匹配
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode (10): Regular Expression Matching [HARD]
https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...
- 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 ...
随机推荐
- BZOJ 2152: 聪聪可可 点分治
2152: 聪聪可可 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.php ...
- delphi 菜单的项目是否可用
菜单的项目是否可用 TPopupMenu.OnPopup事件 把代码放在这里面判断 // ----------------------------------------------- ...
- 【剑指offer】近期公共祖先
转载请注明出处:http://blog.csdn.net/ns_code/article/details/28113959 剑指offer上的最后一题了,一个递归函数调了一下午,才得到正确的结果. 题 ...
- Asp.Net+Extjs实现登录
通过对Ext的学习,发现学习分三部曲:1.看官网的Demo,宏观了解Ext能做什么:2.看相关书籍,做理论指导:3.实现官网的Demo,体会Ext的真谛. 在完毕了第一.二部后,如今我们须要做的是实现 ...
- Step-by-Step XML Free Spring MVC 3 Configuration--reference
The release of Spring 2.5 reduce the burden of XML by introduction annotation based configuration, b ...
- PHP.6-PHP环境搭建(Windows环境下)-LAMP
PHP环境搭建(Windows环境下)-LAMP Windows系统上分别独立安装Apache2.PHP5.MySQL5和phpMyAdmin等几个软件.独立安装的好处是可以自由选择这些组件的具体版本 ...
- JavaFX(一)窗口跳转
笔者此处不讲JavaFX的基础API,只针对笔者工作时遇到的问题进行记录与总结. 零基础的网友可以访问http://www.javafxchina.net/blog/docs/tutorial1/进行 ...
- 关于Servlet会话跟踪的那些事儿
关于servlet会话跟踪,一搜都能搜出很多.我也不免落入俗套,也总结了一把.希望我所总结的知识尽量是知识海洋里的一汪清泉.能帮助到我自己和哪怕一个人,那也是值得的. 故事由来: 我们知道,http协 ...
- scrapy yield Request
import scrapy from myproject.items import MyItem class MySpider(scrapy.Spider): name = ’example.com’ ...
- github/hexo搭建个人博客几个问题总结
问题一:hexo ERROR Deployer not found: github or hexo ERROR Deployer not found: git npm install hexo-dep ...