[LeetCode] Regular Expression Matching 解题报告
来源:互联网 发布:input onchange php 编辑:程序博客网 时间:2024/06/17 21:15
[题目]
mplement 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
[中文翻译]
实现正则表达式匹配,支持’.’和’*’。
‘.’ 匹配任何单个字符.
‘*’ 匹配0个或多个前导字符.需要匹配整个输入的字符串(不是部分).
函数原型如下:
bool isMatch(const char *s, const char *p)例子:
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
[解题思路]
普通的字符就一一匹配。
‘.’比较好处理,匹配任意字符。
麻烦的是’*’,使用递归,依次枚举’*’所代表的个数。
思路大致如此,代码实现地比较烦,是我很早以前写的,又没有写注释。
最近很烦,也不想补注释了,谅解。
[C++代码]
class Solution {private: string ts, tp; int slen, plen;public: bool isMatch(string s, string p) { ts = s; tp = p; slen = s.size(); plen = p.size(); return myMatch(0, 0); } bool myMatch(int ps, int pp) { if (ps == slen && pp == plen) return true; if (ps < slen && pp == plen) return false; if (pp + 1 < plen) { if ('*' == tp.at(pp + 1)) { for (int i = ps; i < slen; i++) { if (ts.at(i) == tp.at(pp) || '.' == tp.at(pp)) { if (myMatch(i + 1, pp + 2)) return true; } else break; } return myMatch(ps, pp + 2); } else { if (ps == slen) return false; if (ts.at(ps) == tp.at(pp) || '.' == tp.at(pp)) return myMatch(ps + 1, pp + 1); else return false; } } else { if (ps == slen) return false; if (ts.at(ps) == tp.at(pp) || '.' == tp.at(pp)) return myMatch(ps + 1, pp + 1); else return false; } }};
- [leetcode] 10. Regular Expression Matching 解题报告
- [Leetcode] 10. Regular Expression Matching 解题报告
- [LeetCode] Regular Expression Matching 解题报告
- [LeetCode 解题报告]010.Regular Expression Matching
- (待解决)LeetCode 10. Regular Expression Matching 解题报告
- 【LeetCode】10.Regular Expression Matching(hard)解题报告
- 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
- Regular Expression Matching LeetCode
- LeetCode | Regular Expression Matching
- LeetCode: Regular Expression Matching
- Leetcode: Regular Expression Matching
- [LeetCode] Regular Expression matching
- Python程序打包--pyinstaller库的使用
- 生成唯一的订单号
- 给出一个包含 0 .. N 中 N 个数的序列,找出0 .. N 中没有出现在序列中的那个数。
- Java语言输出2到100之间的质数
- 技术宅找女朋友的技术分析
- [LeetCode] Regular Expression Matching 解题报告
- docker打包informix镜像
- 一个java泛型的例子
- 多线程实现服务器与多个客户端通信
- Elasticsearch Reference [5.2] » Setup Elasticsearch
- bzoj 3172: [Tjoi2013]单词 后缀数组+rmq+二分
- Python + Graphviz绘制神经网络结构图--简化版本实现
- bzoj1012: [JSOI2008]最大数maxnumber
- iOS C++ 关于深度学习(机器学习) (一)学习概览