LeetCode—10.Regular Expression Matching
来源:互联网 发布:qt编程html5教程 编辑:程序博客网 时间:2024/06/05 07:49
思路:注意一点,*表示前一个字符重复任意次(可以为0次),所以反向匹配,先判断最后一个是不是*,如果是*,再判断前面不是.,然后递归判断。
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()%2!=0) {return false;}char[] c=p.toCharArray();for (int i = 1; i < c.length; i+=2) {if (c[i]!='*') {return false;}}return true;} int pl=p.length(); int sl=s.length(); String ep=p.substring(pl-1,pl); if (ep.equals("*")) {if (p.substring(pl-2,pl-1).equals(".")) {for (int i = sl-1; i >=0-1; i--) {if (isMatch(s.substring(0, i+1),p.substring(0, pl-2))) {return true;}}return false;}else {String k=p.substring(pl-2,pl-1);for (int i = sl-1; i >=0; i--) {String t=s.substring(i,i+1);if (t.equals(k)) {if (isMatch(s.substring(0, i+1),p.substring(0, pl-2))) {return true;}}else {return isMatch(s.substring(0, i+1),p.substring(0, pl-2));}}return isMatch("",p.substring(0, pl-2));}}else if(ep.equals(".")){return isMatch(s.substring(0, sl-1),p.substring(0, pl-1));}else {String es=s.substring(sl-1,sl);if (ep.equals(es)) {return isMatch(s.substring(0, sl-1),p.substring(0, pl-1));}return false;} }}
0 0
- LeetCode—10.Regular Expression Matching
- [LeetCode]10.Regular Expression Matching
- LeetCode --- 10. Regular Expression Matching
- [Leetcode] 10. Regular Expression Matching
- [LeetCode]10.Regular Expression Matching
- [leetcode] 10.Regular Expression Matching
- Leetcode-10.Regular Expression Matching
- leetcode 10. Regular Expression Matching
- leetcode 10. Regular Expression Matching
- Leetcode 10. Regular Expression Matching
- leetcode 10. Regular Expression Matching
- Leetcode 10. Regular Expression Matching
- leetcode 10. Regular Expression Matching
- LeetCode-10. Regular Expression Matching
- leetcode.10. Regular Expression Matching
- LeetCode 10. Regular Expression Matching
- leetcode 10. Regular Expression Matching
- Leetcode-10. Regular Expression Matching
- 【xampp】在phpmyadmin中修改了用户的密码,登录不了数据库
- Android 属性动画(Property Animation) 完全解析 (上)
- JavaScript中的类和原型的理解
- ListView 显示(数据变化后滚动到)最底部。
- WinAPI:ChooseColor 和 CFHookProc
- LeetCode—10.Regular Expression Matching
- Eclipse自动编译NDK/JNI的三种方法
- zabbix英文模式下“ 数据包计划程序微型端口”乱码解决
- NSBundle pathForResource取不到值问题
- TIOBE 2015年10月编程语言排行榜
- CSS3 经典教程系列:CSS3 线性渐变(linear-gradient)
- android touch事件分发
- Unity3D摄像机跟随物体移动的代码控制
- Scala教程(十六)Scala复合类型与依赖注入详解