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 ...
随机推荐
- Javascript实现CheckBox的全选与取消全选的代码(转)
js函数 复制代码 代码如下: <script type="text/javascript"> function checkAll(name) { var el = d ...
- Java中文档制作与继承
1:如何制作帮助文档(了解) (1)写一个类 (2)加入文档注释 (3)通过javadoc工具生成即可 javadoc -d 目录 -author -version ArrayTool.java 2: ...
- Android应用增加计时器
昨天写的Sudoku游戏需要增加计时器功能,使用Chronometer实现如下,由于Chronometer自己在调用stop之后后台的计时器还会继续增加,所以暂停功能需要额外实现: 在StartAct ...
- GIT 如何删除某个本地的提交
一.rm后要commit一下才会生效,但这样只是让文件不再出现在今后的版本中,文件副本仍然会在.git/下(这样git才能让误删的文件恢复). 要彻底消灭文件副本,那就要让文件彻底从历史中消失,分两种 ...
- sass安装 使用
一 什么是sass sass是一种css开发工具.提供了很多便利的写法,使得css开发变得简单 易维护 sass有两种后缀名文件:一种后缀名为sass,不使用大括号和分号:另一 ...
- 线程和NSThread 、 NSOperation
1 使用NSThread实现打地鼠 1.1 问题 NSThread是ios提供的轻量级的多线程解决方案,但是需要自己管理线程的生命周期.线程同步等.本案例使用NSThread实现一个打地鼠的小游戏,在 ...
- LPTHW 笨方法学python 19章
本章节,我只是把所有的输出加上了自己的注释. #!/usr/bin/env python # -*- coding:utf-8 -*- def cheese_and_crakers(cheese_co ...
- CI框架源码分析
这几天,把ci源码又看了一遍,于是有了新的收获.明白了在application目录下core文件夹的作用,就是用来写ci核心文件的扩展的, 而且需要在配置文件中添加类前缀MY_. CI框架整体是但入口 ...
- 引用计数gc机制使用不当导致内存泄漏
上一篇文章找同事review了一下,收到的反馈是铺垫太长了,我尽量直入正题,哈哈 最近dbd压测时发现内存泄漏,其实这个问题去年已经暴露了,参见这篇博客[压测周].当时排查不够仔细,在此检讨下.关于d ...
- 腾讯优测优分享 | 探索react native首屏渲染最佳实践
腾讯优测是专业的移动云测试平台,旗下的优分享不定时提供大量移动研发及测试相关的干货~ 此文主要与以下内容相关,希望对大家有帮助. react native给了我们使用javascript开发原生app ...