Java [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.
http://i.cnblogs.com/EditPosts.aspx?opt=1
The matching should cover the entire input string (not partial). The function prototype should be:
bool isMatch(const char *s, const char *p) Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true
解题思路:
从字符串s和p的位置sp,pp处分别开始匹配,运用递归的方法不断缩小字符串的比较长度,最后回溯回来比较结果。
假设现在走到分别走到sp,pp处。则分为下面几种情况:
(1)如果pp的位置已经越界,那么若sp也已越界,那么则返回true;否则说明s字符串还有长度,不匹配,返回false;
(2)pp的位置为p的最后一位,那么此时只能一一匹配,若不匹配则返回false;否则进行下一位比较;
(3)pp的位置比较靠前,那么比较pp的下一位。如果下一位不是'*',则只能一一匹配,若不匹配则返回false;否则进行下一位比较。如果下一位是'*',则将pp的位置向后移动两位,sp的位置从现在开始进行暴力比较,每次向后移一位进行递归比较。
代码如下:
public class Solution {
public static boolean isMatch(String s, String p) {
if (s == null)
return p == null;
if (p == null)
return s == null;
return helper(s, p, 0, 0);
}
private static boolean helper(String s, String p, int sp, int pp) {
if (pp >= p.length())
return sp >= s.length();
// pp comes to end
if (pp == p.length() - 1) {
if (sp >= s.length()
|| (s.charAt(sp) != p.charAt(pp) && p.charAt(pp) != '.'))
return false;
else
return helper(s, p, sp + 1, pp + 1);
}
// p.charAt(pp+1)!='*'
if (p.charAt(pp + 1) != '*') {
if (sp >= s.length()
|| (s.charAt(sp) != p.charAt(pp) && p.charAt(pp) != '.'))
return false;
else
return helper(s, p, sp + 1, pp + 1);
}
// p.charAt(pp+1)=='*'
while (sp < s.length()
&& (p.charAt(pp) == '.' || s.charAt(sp) == p.charAt(pp))) {
if (helper(s, p, sp, pp + 2))
return true;
sp++;
}
return helper(s, p, sp, pp + 2);
}
}
Java [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 [Difficulty: Hard]
题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single charac ...
- [LeetCode] 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...
- [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 ...
随机推荐
- php5函数库
* APC缓存 apc_add — 缓存一个变量到数据存储 * DateTime DateTime::addDateTime::diffDateTime::formatDateTime::modify ...
- win系统一键安装JDK和Tuxedo
@echo off title JDK和tuxedo环境变量设置 color 0a set /p inputTUX= [请输入你要设置的tuxedo的安装目录:] if /i "%input ...
- make问题:make[1] entering directory
执行make distclean命令.
- touches获得手指点击的坐标
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObjec ...
- linux tail
tail 命令从指定点开始将文件写到标准输出,使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不但刷新, ...
- unity脚本的基础语法
基本的回调方法 Strat()方法:在游戏场景加载时被调用,在该方法内可以写一些游戏场景初始化之类的代码. update():在每一帧渲染之前被调用,大部分游戏代码在这里执行,除了物理部分的代码. F ...
- [转载]C#对象序列化与反序列化
文章写的实在是太好了,忍不住转来: http://www.cnblogs.com/LiZhiW/p/3622365.html#_Toc8478 1.对象序列化的介绍 (1).NET支持对象序列化的几种 ...
- Extjs布局——layout: 'card'
先看下此布局的特性: 下面演示一个使用layout: 'card'布局的示例(从API copy过来的)——导航面板(注:导航面板切换下一个或上一个面板实际是导航面板的布局--layout调用指定的方 ...
- 基于局部敏感哈希的协同过滤算法之simHash算法
搜集了快一个月的资料,虽然不完全懂,但还是先慢慢写着吧,说不定就有思路了呢. 开源的最大好处是会让作者对脏乱臭的代码有羞耻感. 当一个做推荐系统的部门开始重视[数据清理,数据标柱,效果评测,数据统计, ...
- Windows2003/2008/2008 R2下易语言点支持库配置就退出的问题
问题: 请问一个问题,我的电脑上win2003系统的,安装了易语言后,一点支持库配置就会自动退出.这是为什么啊? 解决方法如下: 删除 lib下的wmp.npk,重新打开易语言就可以了.