10、Regular Expression Matching
来源:互联网 发布:淘宝兴业银行进不去 编辑:程序博客网 时间:2024/05/18 03:44
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
class Solution {public: bool isMatch(const char *s, const char *p) { //此方法是网上的一个比较简单的方法,原理与下面的方法差不多,没通过,不想研究了,以后研究吧 /* if (*p == '\0') return *s == '\0'; if (*(p + 1) != '*') { return ((*p == *s) || (*p == '.' && *s != '\0')) && isMatch(s + 1, p + 1); } while ((*p == *s) || (*p == '.' && *s == '\0')) { if (isMatch(s, p + 2)) return true; s++; } return isMatch(s, p + 2); */ } /*bool isMatch(const char *s, const char *p) { //刚开始题目有些理解错误,把'*'理解为匹配任意并任意多的字符,在此题中是匹配任意多的前缀字符,注意题目意思,个人感觉此方法不是最优的,存在重复计算,更优的解留给以后 int len1 = strlen(s), len2 = strlen(p); return ismatch(s, p, 0, 0, len1, len2); } bool ismatch(const char *s, const char *p, int index1, int index2, int length1, int length2) { int i = index1, j = index2; while (i < length1 && j < length2) { //首先我把p[j]分为两种情况,一种是后面带'*', 一种是独立的字母的 if (j + 1 < length2 && p[j+1] == '*') { if (ismatch(s, p, i, j + 2, length1, length2) == false) { if (s[i] == p[j] || p[j] == '.') ++i; else return false; } else return true; } else { if (s[i] == p[j] || p[j] == '.') { ++i; ++j; } else return false; } } while (j + 1 < length2 && p[j + 1] == '*') j += 2; if (i == length1 && j == length2) return true; else return false; }*/};
0 0
- 10、Regular Expression Matching
- 10,Regular Expression Matching
- 10Regular Expression Matching
- 10 Regular Expression Matching
- 10、Regular Expression Matching
- 10Regular Expression Matching
- 【10】Regular Expression Matching
- 10-Regular Expression Matching
- Leetcode【10】:Regular Expression Matching
- [leetcode 10] Regular Expression Matching
- LeetCode(10) Regular Expression Matching
- [leetcode 10] Regular Expression Matching
- leetcode.10------------Regular Expression Matching
- [leetcode] 10 Regular Expression Matching
- [leetcode] 10 Regular Expression Matching
- LeetCode 10:《Regular Expression Matching》
- leecode 10 Regular Expression Matching
- leetcode 10: Regular Expression Matching
- ubuntu adb 无法识别手机
- Codeforces Round #295 (Div. 2) -- B. Two Buttons (模拟)
- 趋势科技实习--3个tool的实现
- kbengine引擎登陆流程分析
- advertisement总结
- 10、Regular Expression Matching
- 让 python 命令行也自动补全
- javascript学习之函数章节
- MITx: 6.00.1x Alphabetical Substrings (python)
- POJ 2115解题报告
- Java内存回收机制全解(算法+内存分配)
- GitHub上最火的40个Android开源项目
- bzoj 1095 捉迷藏(线段树)
- MJRefresh objc_msgSend 报错解决方案