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 ...
随机推荐
- EditText 数字范围 检查string 是不是数字
public static boolean isNumeric00(String str){ try{ Integer.parseInt(str); return true; }catch(Numbe ...
- 模板 倍增维护RMQ
倍增维护RMQ,nlogn预处理,O(1)查询 #include<bits/stdc++.h> using namespace std; const int maxn = 1e5+7; s ...
- HDU 3472 HS BDC (混合图的欧拉路径判断)
HS BDC Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- J1850 Implement
http://avrobdii.googlecode.com/svn/trunk/code/J1850.c /* Copyright (C) Trampas Stern name of author ...
- STM32F4 Alternate function mapping
#define GPIO_AF0_MCO // MCO (MCO1 and MCO2) Alternate Function mapping #define GPIO_AF0_RTC_50Hz // ...
- AT91 USB Composite Driver Implementation
AT91 USB Composite Driver Implementation 1. Introduction The USB Composite Device is a general way t ...
- 【Go命令教程】2. go build
go build 命令用于编译我们 指定的 源码文件 或 代码包 以及它们的依赖包. 例如,如果我们在执行 go build 命令时不后跟任何代码包,那么命令将试图编译当前目录所对应的代码包.例如, ...
- 使用Oracle DBLink进行数据库之间对象的訪问操作
Oracle中自带了DBLink功能,它的作用是将多个oracle数据库逻辑上看成一个数据库,也就是说在一个数据库中能够操作还有一个数据库中的对象,比如我们新建了一个数据database1.我们须要操 ...
- Win10专业版永久激活方法
自从升级安装了Windows10系统以后,我想很多朋友和我一样,想要激活Windows10系统,但是小编找了半天以后发现,很多激活工具都是批量激活的,也就是只有180天的使用时间,那么我们怎么永久激活 ...
- U盘装win7系统
首先在互联网下载UltraISO光盘映像文件制作/编辑/格式转换工具,(当然还有其它如WinISO.WinImage.Daemon Tools等)然后在准备一个4GB容量以上(含4GB)的优盘或者移动 ...