欢迎使用CSDN-markdown编辑器
2015-07-19 09:25
706 查看
题目来源:https://leetcode.com/problems/regular-expression-matching/
Regular Expression Matching
‘.’匹配任意单个字符,‘*’匹配0个或多个前一字符。如果匹配整个串返回true。
例:
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
解决方案转载自:/article/5002355.html
Regular Expression Matching
‘.’匹配任意单个字符,‘*’匹配0个或多个前一字符。如果匹配整个串返回true。
例:
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
解决方案转载自:/article/5002355.html
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; }
相关文章推荐
- C# 条件与&&与条件或||的使用总结
- Tree Recovery-UVA 536
- 那些神奇的"大师"是如何炼成的(科学、迷信、心理)
- Android Fragment
- 敏捷开发之Scrum扫盲篇
- 嵌入式学习答疑
- Android之——Handler随笔
- Merge Two Sorted Lists
- poj1649 Rescue(BFS+优先队列)
- (win8系统)VMware-workstation安装教程(图文)
- java虚拟机学习笔记 一
- 《textanalytics》课程简单总结(4):课程总结
- 三次面试总结
- [LeetCode] Longest Palindrome Substring 具体分析
- UIPopoverController和UIActionSheet在iPad中使用
- 7.18使用<a>标签
- cocos2d-x-3.1.1工程创建
- TCP/IP网络简介(来自与51CTO学院视频授课内容)
- 7.16日table学习笔记
- php开发之数组排序