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 5512 Pagodas (gcd)
题目:传送门. 题意:t组数据,每组数据给定n,a,b,a!=b,在[1,n]的这些点中,每次选取a+b或a-b或b-a点,选取过的点在下次选取的时候可以当做ab来用继续选取,谁不能继续选取谁就输,问 ...
- MFC 选择一个文件或者文件夹路径
//选择文件CFileDialog dlg(TRUE, 0, 0, OFN_HIDEREADONLY, "文本文件|*.txt|所有文件|*.*)||",0);if (dlg.Do ...
- 解决 g++ error:/usr/lib/rpm/redhat/redhat-hardened-cc1 No that file and directory
You need to install redhat-rpm-config which is required by some of the qt switches, probably: sudo d ...
- 【读书笔记】读《JavaScript模式》 - 对象创建模式
JavaScript是一种简洁明了的语言,其中并没有在其他语言中经常使用的一些特殊语法特征,比如命名空间(namespace).模块(module).包(package).私有属性(private p ...
- JAVA读取XML文件数据
XML文档内容如下: <?xml version="1.0" encoding="UTF-8"?> <root> <field t ...
- loj 1168(Tarjan应用)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26882 思路:一开始把题意理解错了,还以为是简单路径,然后仔细一看 ...
- 网站构建学习笔记(0)——基本概念了解及资源学习(copy自w3school)
一.学习方面 1.WWW - 万维网 什么是 WWW? WWW 指万维网(World Wide Web) 万维网常被称为Web Web 是由遍布全球的计算机所组成的网络 所有 Web 中的计算机都可以 ...
- SVN学习安装总结
前言: 第一次接触SVN的时候并不是自己学习SVN的时候,而是再看师哥师姐做项目的时候,看着他们打开SVN向上传东西的时候,我就在想,这是个什么东西,需要每个人都安装吗?大胆的我也就问了 ...
- Centos6.4编译安装Node.js(已验证)
1.准备源文件包 [felix@MyTerminal Downloads]$ mv node-6.5.0 node [felix@MyTerminal Downloads]$ ls node node ...
- MongoDB安装(一)
下载 上MongoDB官网 ,我们发现有32bit和64bit,这个就要看你系统了,不过这里有两点注意: ①:根据业界规则,偶数为“稳定版”(如:1.6.X,1.8.X),奇数为“开发版”(如:1.7 ...