Regular Expression Matching
来源:互联网 发布:淘宝 卖东西 编辑:程序博客网 时间:2024/06/07 00:46
题目:
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") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "a*") → trueisMatch("aa", ".*") → trueisMatch("ab", ".*") → trueisMatch("aab", "c*a*b") → true
若p[0] == '\0' ,则判断s[0] == '\0' 是则返回true 否则返回false
若p[1] == '*' 若p[0] == '.' 或者 s[0] == p[0] 则可以选择重复多次匹配s[0]直至不可重复为止,或者一次也不重复若p[1] != '*' 若p[0] == '.' 或者 s[0] == p[0] 则s和p都进一位进行匹配,否则返回false
class Solution {public: bool isMatch(string s, string p) { if(p[0] == '\0') return s[0] == '\0'; if(p[1] == '*') { while((s[0] != '\0' && p[0] == '.') || s[0] == p[0]) { if(isMatch(s, p.substr(2, p.length() - 2))) return true; s = s.substr(1, s.length() - 1); } return isMatch(s, p.substr(2, p.length() - 2)); } else { if((s[0] != '\0' && p[0] == '.') || s[0] == p[0]) return isMatch(s.substr(1, s.length() - 1), p.substr(1, p.length() - 1)); } return false; }};
0 0
- LeetCode: Regular Expression Matching
- LeetCode: Regular Expression Matching
- leetcode Regular Expression Matching
- [Leetcode] Regular Expression Matching
- Regular Expression Matching
- Regular Expression Matching
- [LeetCode]Regular Expression Matching
- Regular Expression Matching
- LeetCode-Regular Expression Matching
- Regular Expression Matching
- Regular Expression Matching LeetCode
- Regular Expression Matching
- LeetCode | Regular Expression Matching
- LeetCode: Regular Expression Matching
- Leetcode: Regular Expression Matching
- [LeetCode] Regular Expression matching
- [LeetCode] Regular Expression Matching
- LeetCode Regular Expression Matching
- C++中浅拷贝和深拷贝问题
- swift Bool和元组(Turple)
- Go1.5 改进摘要
- 【cocos2d-x 3.X自学笔记】[转]Eclipse配置安卓开发环境(解决SDK manager下载慢问题)
- 文章标题
- Regular Expression Matching
- JavaScript学习(二)
- Android:控件WebView显示网页
- 配置LVS + Keepalived高可用负载均衡集群之图文教程
- Android ViewPager放入多个XML如何监听其的控件
- !HDU 1500 Chopsticks-dp-(分组问题)
- Server.UrlEncode、HttpUtility.UrlDecode的区别
- 使用 WordPress 的子主题(Child Themes)功能快速制作自己的主题
- Appium——Windows环境搭建