66. Regular Expression Matching
Regular Expression Matching
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
思路: 这题 ugly 之处,在于 '*' 给的很不明朗, 其实'c*', 表示 '*' 可以代表 'ccc...cc'。
对 '*' 的理解,若出现 p中出现 '*', 则要比较用 s 和 p 中 '*' 之前的那个元素比较,从左往右找出 s 中第一个与 p 之前的元素不同的元素。
(若全相同,例如, 之前那个元素为 '.', 则 '.*' (表示'.......^....')可以匹配任何 s 串)。
//垃圾题!rubbish question!
class Solution {
public:
bool isMatch(const char *s, const char *p) {
if (s == NULL || p == NULL) return false;
if (*p == '\0') return *s == '\0';
// ".*" matches "", so we can't check (*s == '\0') here.
if (*(p + 1) == '*'){
// Here *p != '\0', so this condition equals with
// (*s != '\0' && (*p == '.' || *s == *p)).
while ((*s != '\0' && *p == '.') || *s == *p){
if (isMatch(s, p + 2)) return true;
++s;
}
return isMatch(s, p + 2);
}
else if ((*s != '\0' && *p == '.') || *s == *p){
return isMatch(s + 1, p + 1);
}
return false;
}
};
66. Regular Expression Matching的更多相关文章
- [LeetCode] Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- [LeetCode] 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...
- No.010:Regular Expression Matching
问题: Implement regular expression matching with support for '.' and '*'.'.' Matches any single charac ...
- 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】Regular Expression Matching (hard) ★
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【JAVA、C++】LeetCode 010 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] Regular Expression Matching(递归)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
随机推荐
- enmo_day_06
RAC Data Guard (DG) EMC NAS SAN 双活 数据完整性 约束 : 主键 : 非空 且 唯一 非空 : 唯一 : 外键 : 检查 : DISABLE, ENABLE VALID ...
- CENTOS/UBUNTU一键安装IPSEC/IKEV2 VPN服务器
1.在azure上创建ubuntu虚拟机 选择v15.04 server 版本 2.添加端口号 3.远程桌面到ubuntu 命令行 输入 sudo su 输入创建 ubuntu虚拟机 时候的 密码 ...
- 拿到新机器,进行初始化和部署Nginx的过程
1. 在/etc/ansbile/hosts中添加主机init 2. 在sysinit.yml中修改要初始化的机器: hosts: init 3. 设置不检查key export ANS ...
- 2015-微软预科生计划-面试题-Swimming Plans
http://hihocoder.com/problemset/problem/1188 题目大意 Steven在时刻T到达了室内游泳池. 游泳池一共有N条泳道,游泳池两侧分别标记为0和1. 已知除了 ...
- 说说chrome上的JS调试
步骤:审查元素 ->source->选择js文件 ->设置断点 ->触发函数 ->进入调试点step over 到elements目录下双击某一块代码还可以立刻修改,ct ...
- having()方法设置查询条件,where()设置查询条件
having 和 where区别 ① 使用有先后顺序 ② where price>100 having price>100 ③ where 设置条件,字段必须是数据表中存在的 ...
- OC 属性
1 属性和实例变量 :属性 == 实例变量声明 + setter 方法+ getter 方法 在老版本的 OC 语言中,我们需要同时声明属性和底层实例变量,那时,属性是 OC 语言的一个新的机制,并 ...
- asp.net 自定义控件
在Visual Studio中,所有的ASP.NET 2.0控件都是自定义控件,创建自己的自定义控件一般需要完成以下三步.(1)在站点APP_Code下创建一个新类:(2)修改这个类,让它成为WebC ...
- 龙威零式_团队项目例会记录_18 (Beta架构讨论)
例会照片 任务更新 姓名 今日完成任务 实际花费时间 明日任务 预计花费时间 谢振威 继续构思beta版本架构并且输出文档 2h #40数据库模块接口定义 2h 杨金键 继续构思beta版本架构并且输 ...
- javascript:void(0) ,设置a链接无效,设置点击a页面不刷新,不跳动
http://www.cnblogs.com/opper/archive/2009/01/12/1373971.html 我想使用过ajax的都常见这样的代码: <a href="ja ...