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 ...
随机推荐
- iOS:TabBarController 显示/隐藏第一级页面的TabBar
- (void)setTabBarHidden:(BOOL)hidden { UIView *tab = self.tabBarController.view; ) { return; } UIVie ...
- 关于HTTP Message
HTTP Message包括JS, HTML等Resource.这些都是相对来说有代码可以写的东西,但是原理的东西是没有代码的.coding只是很少的一部分工作内容. Browser的流程.比如con ...
- Nginx访问日志 Nginx日志切割 静态文件不记录日志和过期时间
- Mac或者linux下登陆到linux上的SFTP
登陆 sftp -i 密钥路径 用户@ip ➜ ~ sftp -i Desktop/aliyun.pem root@39.106.30.1 Connected to 39.106.30.1 上 ...
- require.js初试(with angular & optimization)
如果你只是想找一款称手的js加载器,可以参考这篇js loader benchmarks(http://artzstudio.com/files/Boot/test/benchmarks/script ...
- 【RespberryPi】数码管
http://blog.mangolovecarrot.net/2015/06/03/raspi-study0801/ 应该可以用两块74HC595来驱动显示8位数的数码管.
- node配置自动监测文件改变不重启
方法一: nodemon npm install -g nodemon nodemon ./bin/www 或者在npm start命令里把node改为nodemon 方法二:supervisor n ...
- vue父组件向子组件动态传值的两种方法
在一些项目需求中需要父组件向子组件动态传值,比如我这里的需求是,父组件动态通过axios获取返回的图片url数组然后传给子组件,上传图片的子组件拿到该数组后进行遍历并展示图片,因为有时候获取到的会是空 ...
- MyEclipse使用笔记
简单记录下个人常用的一些MyEclipse设置 VS颜色方案 Window-->Preference-->Java->Editor-->Syntax Coloring Clas ...
- Visual Code 调用Chrome 浏览HTML
Code 使用快捷键:Ctrl+Shit+B 然后再Task.json,替换以下: { "version": "0.1.0", "command&qu ...