Regular Expression Matching leetcode java
题目:
Implement regular expression matching with support for '.' and '*'.
'.' Matches any single character.
'*' Matches zero or more of the preceding element. 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 题解:
本文及代码引用自:http://harrifeng.github.io/algo/leetcode/regular-expression-matching.html
- 首先要理解题意:
- "a"对应"a", 这种匹配不解释了
- 任意字母对应".", 这也是正则常见
- 0到多个相同字符x,对应"x*", 比起普通正则,这个地方多出来一个前缀x. x代表的是
相同的字符中取一个,比如"aaaab"对应是"a*b" - "*"还有一个易于疏忽的地方就是它的"贪婪性"要有一个限度.比如"aaa"对应"a*a",
代码逻辑不能一路贪婪到底
- 正则表达式如果期望着一个字符一个字符的匹配,是非常不现实的.而"匹配"这个问题,非
常容易转换成"匹配了一部分",整个匹配不匹配,要看"剩下的匹配"情况.这就很好的把
一个大的问题转换成了规模较小的问题:递归 - 确定了递归以后,使用java来实现这个问题,会遇到很多和c不一样的地方,因为java对字符
的控制不像c语言指针那么灵活charAt一定要确定某个位置存在才可以使用. - 如果pattern是"x*"类型的话,那么pattern每次要两个两个的减少.否则,就是一个一个
的减少. 无论怎样减少,都要保证pattern有那么多个.比如s.substring(n), 其中n
最大也就是s.length()
代码如下:
1 public static boolean isMatch(String s, String p) {
2 if (p.length() == 0)
3 return s.length() == 0;
4
5 // length == 1 is the case that is easy to forget.
6 // as p is subtracted 2 each time, so if original
7 // p is odd, then finally it will face the length 1
8 if (p.length() == 1)
9 return (s.length() == 1) && (p.charAt(0) == s.charAt(0) || p.charAt(0) == '.');
// next char is not '*': must match current character
if (p.charAt(1) != '*') {
if (s.length() == 0)
return false;
else
return (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.')
&& isMatch(s.substring(1), p.substring(1));
}else{
// next char is *
while (s.length() > 0 && (p.charAt(0) == s.charAt(0) || p.charAt(0) == '.')) {
if (isMatch(s, p.substring(2)))
return true;
s = s.substring(1);
}
return isMatch(s, p.substring(2));
}
}
Regular Expression Matching leetcode java的更多相关文章
- Regular Expression Matching leetcode
递归方法运行时间过长.考虑使用动态规划的方法. 代码如下: bool isMatch(string s, string p) { int i,j; int m=s.size(); int n=p.si ...
- lc面试准备:Regular Expression Matching
1 题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single char ...
- 【JAVA、C++】LeetCode 010 Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- Java [leetcode 10] Regular Expression Matching
问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
- LeetCode第[10]题(Java):Regular Expression Matching
题目:匹配正则表达式 题目难度:hard 题目内容:Implement regular expression matching with support for '.' and '*'. '.' Ma ...
- [leetcode]Regular Expression Matching @ Python
原题地址:https://oj.leetcode.com/problems/regular-expression-matching/ 题意: Implement regular expression ...
- [LeetCode] Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- LeetCode | Regular Expression Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
- LeetCode (10): Regular Expression Matching [HARD]
https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...
随机推荐
- C#泛型的抗变与协变
C#泛型的抗变与协变 学习自 C#本质论6.0 https://www.cnblogs.com/pugang/archive/2011/11/09/2242380.html Overview 一直以来 ...
- JavaScript实现链式调用
学习Jquery的时候,我们通常会看到链式调用的写法 $(window).addEvent('load', function(){ $('test').show().setStyle('color', ...
- BZOJ.4456.[ZJOI2016]旅行者(分治 Dijkstra)
题目链接 \(Description\) 给定\(n\times m\)的带边权网格图.\(Q\)次询问从点\((x_i,y_i)\)到点\((x_j,y_j)\)的最短路. \(n\times m\ ...
- bzoj 1171 并查集优化顺序枚举 | 线段树套单调队列
详见vfleaking在discuss里的题解. 收获: 当我们要顺序枚举一个序列,并且跳过某些元素,那么我们可以用并查集将要跳过的元素合并到一起,这样当一长串元素需要跳过时,可以O(1)跳过. 暴力 ...
- Codeforces Round #370 (Div. 2) C. Memory and De-Evolution 水题
C. Memory and De-Evolution 题目连接: http://codeforces.com/contest/712/problem/C Description Memory is n ...
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem H. Hometask 水题
Problem H. Hometask 题目连接: http://codeforces.com/gym/100714 Description Kolya is still trying to pass ...
- CentOS 7搭建KVM在线管理面板WebVirtMgr
系统版本:CentOS 7.4 WebVirtMgr版本:master分支的20180720版本,下载链接(链接:https://pan.baidu.com/s/1kl060hPHDGbwJUR_iM ...
- VirtualBox 在WIN7 X64 安装报错 获取VirtualBox COM对象失败,Unable to start the virtual device
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\CLSID\{---C000-}\InprocServer32] @="C:\ ...
- muduo 的 shutdown() 没有直接关闭 TCP 连接?
http://blog.csdn.net/Solstice/article/details/6208634 今天收到一位网友来信: 在 simple 中的 daytime 示例中,服务端主动关闭时调用 ...
- php简单浏览目录内容
<?php $dir = dirname(__FILE__); $open_dir = opendir($dir); echo "<table border=1 borderCo ...