LeetCode | 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
思路是分别处理a*、.*、.、a的情况。
三周前写的lengthy code如下:
class Solution {
public:
bool isMatch(const char *s, const char *p) {
int sn = strlen(s);
int pn = strlen(p);
return recursive(s, sn, p, pn);
} bool recursive(const char* s, int sn, const char* p, int pn) {
if (sn == && pn == ) return true;
if (pn == ) return false; if (*(p + ) != '\0') {
if (*(p + ) == '*') {
if (*p == '.') { // .*
int n = ;
while (n <= sn) {
if (recursive(s + n, sn - n, p + , pn - )) return true;
n++;
}
} else { // a*
int n = ;
while (n <= sn && *(s + n) == *p) {
if (recursive(s + n, sn - n, p + , pn - )) return true;
n++;
}
if (recursive(s + n, sn - n, p + , pn - )) return true;
}
} else {
if (*p != '.' && *s != *p) return false;
if (recursive(s + , sn - , p + , pn - )) return true;
}
} else {
if (*p != '.' && *s != *p) return false;
if (recursive(s + , sn - , p + , pn - )) return true;
}
}
};
今天看了Leetcode上1337的代码真是羞愧啊。http://leetcode.com/2011/09/regular-expression-matching.html
重写了一遍。思路还是一样。
class Solution {
public:
bool isMatch(const char *s, const char *p) {
if (*p == '\0') return (*s == '\0');
// match single '\0', '.', 'a'...
if (*(p + ) != '*') {
return ((*s == *p || (*p == '.' && *s != '\0')) && isMatch(s + , p + ));
} // match a*, .*
while ((*s == *p || (*p == '.' && *s != '\0'))) {
if (isMatch(s++, p + )) return true;
} // ignore a*, *p != '.' && *s != *p
return isMatch(s, p + );
}
};
LeetCode | Regular Expression Matching的更多相关文章
- [LeetCode] Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- [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 [6]
称号: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- LeetCode——Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- Leetcode:Regular Expression Matching分析和实现
题目大意是要求我们实现一个简单的正则表达式全匹配判断.其中正则表达式中只包含一般字符,以及全匹配字符.和变长字符*.其中.可以匹配一个字符,而*与前一个字符相关联,x*可以被看作任意多个x(0到正无穷 ...
- LeetCode Regular Expression Matching 网上一个不错的实现(非递归)
'.' Matches any single character.'*' Matches zero or more of the preceding element. The matching sho ...
- LeetCode: Regular Expression Matching 解题报告
Roman to IntegerGiven a roman numeral, convert it to an integer. Input is guaranteed to be within th ...
- lc面试准备:Regular Expression Matching
1 题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single char ...
随机推荐
- hdu 1160 FatMouse's Speed 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 题目意思:给出一堆老鼠,假设有 n 只(输入n条信息后Ctrl+Z).每只老鼠有对应的weigh ...
- AJAX 异步交互基本总结
AJAX (Asynchronous JavaScript and Xml) 直译中文 - javascript和XML的异步 同步与异步的区别: 同步交互 执行速度相对比较慢 响应的是完整的HTML ...
- 【python】队列
来源:http://www.cnblogs.com/yupeng/p/3413852.html 关于队列 >>> from collections import deque > ...
- WIN7实现多人远程一台电脑
今天查了查网,发现有人说,WIN7可以实现多人远程一台电脑,于是乎我就试了试, 在工作办公室里的局域网里试了试,嘿,成功了,愿与大家分享一下,呵呵! 方法一: 多用户早就能破解了 方法如下:用UE打开 ...
- web应用配置
tomcat 的 server.html 配置文件 加在</Host>之上 <Context path=”/itcast” docBase=”c:\news” /> path虚 ...
- Accumulators
copy的官方programming guide的代码,只有一个要说一下,Accumulators的更新是在action做的,transformation操作中对于Accumulators的增加不会即 ...
- HTML-Canvas03
颜色合成 globalCompositeOperation 属性: //先绘制一个图形. ctx.fillStyle = "#00ff00"; ctx.fillRect(10,10 ...
- node相关--测试
测试: assert模块; //node自带 var assert = require('assert'); var now = Date.now(); console.log(now); asser ...
- ubuntu 重置密码
背景:在登陆ubuntu之后,按Ctr+Alt+F1进入控制台时,需要登陆,一时忘了密码... 参考:http://www.cnblogs.com/relaxgirl/p/3179507.html ...
- HDU2167 Pebbles(状压DP)
题目给一张n×n的格子,每个格子都有数字,要从格子中取若干个数字,八个方向相邻的数字不能一起取,问取的数字最大和是多少. 从第一行一行一行看下去,可以发现第1行取哪几列只会影响到第2行,第3行后面的一 ...