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 ...
随机推荐
- ArduinoYun教程之OpenWrt-Yun与CLI配置Arduino Yun
ArduinoYun教程之OpenWrt-Yun与CLI配置Arduino Yun OpenWrt-Yun OpenWrt-Yun是基于OpenWrt的一个Linux发行版.有所耳闻的读者应该听说他是 ...
- 用python做一个图片验证码
看一下做出来的验证码长啥样 验证码分析 1. 有很多点 2. 有很多线条 3. 有字母,有数字 需要用到的模块: 1. random 2. Pillow (python3中使用pillow) 安装p ...
- android 四大组件
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 活动,服务,广播接受者,内容提供者. 活动 能够提供 用户界面.服务 没有用户界面.广 ...
- 负载均衡介绍及Nginx简单实现
负载均衡介绍及Nginx简单实现 负载均衡 负载均衡介绍及Nginx简单实现 1. 介绍 2. 常用的开源软件 2.1 LVS 优点 缺点 2.2 Nginx 优点 缺点 3. 常用的开源反向代理软件 ...
- CentOS 7使用yum安装PHP5.6
删除旧php包 yum remove php.x86_64 php-cli.x86_64 php-common.x86_64 php-gd.x86_64 php-ldap.x86_64 php-mbs ...
- Android中使用隐藏API(大量图解)
Android SDK的很多API是隐藏的,我无法直接使用.但是我们通过编译Android系统源码可以得到完整的API. 编译Android系统源码后可以在out\target\common\obj\ ...
- Visual studio 2010出现“error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏”解决方式
本来自己的电脑上装了VS2010,因为开发Cocos2d-x 3.x,所以就在自己的机器上装了一个VS2012. 但是.这不装不要紧,debug一下自己原来的程序,结果出现了"error L ...
- LOG EXPLORER ApexSQL Log
http://www.minisoft.cn/ https://www.apexsql.com/sql_tools_log.aspx
- webpack原理与实战
webpack是一个js打包工具,不一个完整的前端构建工具.它的流行得益于模块化和单页应用的流行.webpack提供扩展机制,在庞大的社区支持下各种场景基本它都可找到解决方案.本文的目的是教会你用we ...
- iOS 项目一直在后台执行
我后来是这么解决不知道行不行,能够长期的在后台执行 首先我在xx-info.plist 里的 "Required background modes" 里增加"App pr ...