LeetCode--No.010 Regular Expression Matching
10. Regular Expression Matching
- Total Accepted: 89193
- Total Submissions: 395441
- Difficulty: Hard
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://www.tuicool.com/articles/Ebiymu
- 首先要理解题意:
- "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()
public boolean isMatch(String s, String p) {
// base case
if (p.length() == 0) {
return s.length() == 0;
}
// special case
if (p.length() == 1) {
// if the length of s is 0, return false
if (s.length() < 1) {
return false;
}
//if the first does not match, return false
else if ((p.charAt(0) != s.charAt(0)) && (p.charAt(0) != '.')) {
return false;
}
// otherwise, compare the rest of the string of s and p.
else {
return isMatch(s.substring(1), p.substring(1));
}
}
// case 1: when the second char of p is not '*'
if (p.charAt(1) != '*') {
if (s.length() < 1) {
return false;
}
if ((p.charAt(0) != s.charAt(0)) && (p.charAt(0) != '.')) {
return false;
} else {
return isMatch(s.substring(1), p.substring(1));
}
}
// case 2: when the second char of p is '*', complex case.
else {
//case 2.1: a char & '*' can stand for 0 element
if (isMatch(s, p.substring(2))) {
return true;
}
//case 2.2: a char & '*' can stand for 1 or more preceding element,
//so try every sub string
int i = 0;
while (i<s.length() && (s.charAt(i)==p.charAt(0) || p.charAt(0)=='.')){
if (isMatch(s.substring(i + 1), p.substring(2))) {
return true;
}
i++;
}
return false;
}
}
LeetCode--No.010 Regular Expression Matching的更多相关文章
- 【LeetCode】010. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 《LeetBook》leetcode题解(10): Regular Expression Matching——DP解决正则匹配
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【JAVA、C++】LeetCode 010 Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- LeetCode 010 Regular Expression Matching
题目描述:Regular Expression Matching Implement regular expression matching with support for '.' and '*' ...
- [Leetcode][Python][DP]Regular Expression Matching
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...
- 【一天一道LeetCode】#10. Regular Expression Matching
一天一道LeetCode系列 (一)题目 Implement regular expression matching with support for '.' and '*'. '.' Matches ...
- 010 Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'.'.' Matches any single character. ...
- No.010 Regular Expression Matching
10. Regular Expression Matching Total Accepted: 89193 Total Submissions: 395441 Difficulty: Hard Imp ...
- 【leetcode】10.Regular Expression Matching
题目描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
- leetcode problem 10 Regular Expression Matching(动态规划)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
随机推荐
- PXC 搭建高可用集群
(1).PXC集群注意事项 1.PXC集群只支持innodb引擎 2.
- jieba分词/jieba-analysis(java版)
简介 支持分词模式Search模式,用于对用户查询词分词Index模式,用于对索引文档分词特性支持多种分词模式全角统一转成半角用户词典功能conf 目录有整理的搜狗细胞词库因为性能原因,最新的快照版本 ...
- node.js中通过stream模块实现自定义流
有些时候我们需要自定义一些流,来操作特殊对象,node.js中为我们提供了一些基本流类. 我们新创建的流类需要继承四个基本流类之一(stream.Writeable,stream.Readable,s ...
- Django contenttypes 应用
Django contenttypes 应用 什么是Django ContentTypes? Django ContentTypes是由Django框架提供的一个核心功能,它对当前项目中所有基于Dja ...
- Linux驱动之同步、互斥、阻塞的应用
同步.互斥.阻塞的概念: 同步:在并发程序设计中,各进程对公共变量的访问必须加以制约,这种制约称为同步. 互斥机制:访问共享资源的代码区叫做临界区,这里的共享资源可能被多个线程需要,但这些共享资源又不 ...
- HTML中关于class内容空格多类名的问题详解
之所以想谈谈这个,不明所以.所以转载下来方便自己看看. 问:像 class="info fl" 这种class定义是何意思? 答:这里的空格隔开后,它们所代表的是两个类名,分别为i ...
- CSS学习总结4:派生选择器学习总结
派生选择器:通过依据元素在其位置的上下文关系来定义样式,你可以使标记更加简洁.派生选择器中一共分为三种:后代选择器.子元素选择器.相邻兄弟选择器. 1.初识派生选择器 实例:你希望列表中的 stron ...
- CentOS_mini下安装docker 之 安装docker CE
警告:切勿在没有配置 Docker YUM 源的情况下直接使用 yum 命令安装 Docker. 原文地址:https://yeasy.gitbooks.io/docker_practice/inst ...
- SVN忘记登陆用户
C:\Users\Yaolz\AppData\Roaming\Subversion\auth 删除里面所有文件
- 搭建django
虚拟环境配置 pip install virtualenv 一般需要安装一个管理工具:pip install virtualenvwrapper-win 只有它可以使用workon 创建虚拟环境:mk ...