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 ...
随机推荐
- array_multisort—对多个数组或多维数组进行排序
From: http://www.cnblogs.com/lwbqqyumidi/archive/2013/01/31/2887188.html PHP中array_multisort可以用来一次对多 ...
- win7下安装双系统Ubuntu14.04后开机没有win7,直接进入Ubuntu
开机进入Ubuntu后,打开命令端,输入: sudo update-grub 然后重启,则解决问题
- python实现原地刷新方式输出-可用于百分比进度显示输出
方式1: import sys sys.stdout.write('\r' + '你的输出详情') sys.stdout.flush() 方式2: print('\r' + '你的输出详情', end ...
- c++ int string互转
1. ){ monthNumber = "; itoa(i+,iCharArray,); monthNumber += iCharArray; } else { itoa(i+,iCharA ...
- 长姿势 教你在qq空间上显示iPhone6尾巴
下午刚午休完的时候,广州很多童鞋都感受到了震感,半青也感受到了,不仅如此,我还感受到了更大震感,那就是翻一下QQ空间动态,竟然看到有一位好友的尾巴竟然显示为“iPhone6”,顿时觉得该好友逼格太高了 ...
- [转]POI实现读写Excel2007完整示例
http://blog.csdn.net/little_stars/article/details/8210532 流程:(跟jxl相似,只是读取逻辑有点不同) 跟jxl的两处主要区别: 1.读取和写 ...
- 黏性Session和非黏性Session
黏性Session和非黏性Session黏性Session:此模式下同一会话中的请求都被派送到同一个tomcat实例上,这样我们就无须在多台服务器之间实现session共享了,这是其好处,不好的地方就 ...
- Android开发学习笔记-splash画面的显示
贴代码: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=&qu ...
- GridFS实现原理
GridFS在数据库中,默认使用fs.chunks和fs.files来存储文件. 其中fs.files集合存放文件的信息,fs.chunks存放文件数据. 一个fs.files集合中的一条记录内容如下 ...
- ./configure、make、make install 命令
https://www.cnblogs.com/tinywan/p/7230039.html https://www.sohu.com/a/191735643_505857 ./configure 该 ...