(leetcode #10)Regular Expression Matching --递归法
2016-01-28 12:06
162 查看
Implement regular expression matching with support for
分析:
(1)注意左边是常规字符串,只有右边是正则。开始我认为左边也可能是正则,形如“a*ab” “b*c”,那就太复杂了。还好此题只有右边是正则。
(2)正则表达式默认合法,即不会出现 **ab.***这种歧义的非法正则
(3)左边以一个字母为单位,右边则有可能是2个字母或者一个字母为单位,这样用递归可以解决,思路上也算比较清晰
(4)如果用C++写,那么string可能存在字串拷贝的情况,所以复杂度上略有冗余。可以用const string&防止拷贝的发生
(5)整体思路:从左边取出最前字符,再从右边取出第一个字符比较,右边的字符可能有以下几种:a . a* .* 。因此只需要对这几种情况进行讨论然后递归即可(复杂度当然较高,不过先解决问题再说)
好了,程序由于用的递归,其实思路还算比较清晰。测试了一下,能够击败34%的人。貌似还算一个不错的结果。但是发现大部分人的耗时非常短,所以肯定用了其他方法,而且hint也提示了,用dp。以后在研究。
'.'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
分析:
(1)注意左边是常规字符串,只有右边是正则。开始我认为左边也可能是正则,形如“a*ab” “b*c”,那就太复杂了。还好此题只有右边是正则。
(2)正则表达式默认合法,即不会出现 **ab.***这种歧义的非法正则
(3)左边以一个字母为单位,右边则有可能是2个字母或者一个字母为单位,这样用递归可以解决,思路上也算比较清晰
(4)如果用C++写,那么string可能存在字串拷贝的情况,所以复杂度上略有冗余。可以用const string&防止拷贝的发生
(5)整体思路:从左边取出最前字符,再从右边取出第一个字符比较,右边的字符可能有以下几种:a . a* .* 。因此只需要对这几种情况进行讨论然后递归即可(复杂度当然较高,不过先解决问题再说)
class Solution { public: bool isMatch(string s1, string s2) { return isMatch2(s1,s2,0,0); } bool isMatch2(const string& s1, const string& s2, int idx1,int idx2 ) { //if to the end of s1 if(s1[idx1] == '\0'){ if(s2[idx2] == '\0') return true; else if(s2[idx2 + 1] == '\0') return false; else{ if(s2[idx2 + 1] == '*') return isMatch2(s1,s2,idx1,idx2 + 2); else return false; } } //if to the end of s2 if(s2[idx2] == '\0') return false; else if(s2[idx2 + 1] == '\0'){ if(s2[idx2] == s1[idx1] || s2[idx2] == '.') return isMatch2(s1,s2,idx1 + 1,idx2 + 1); else return false; } //normal scenrio char c1 = s1[idx1]; char c2 = s2[idx2]; char c2_next = s2[idx2 + 1]; if(c2 != '.'){ if(c2_next != '*'){ if(c1 == c2){//eg:a ab return isMatch2(s1,s2,idx1 + 1,idx2 + 1); } else{//eg:a cb return false; } } else{ if(c1 == c2){//eg:a a* return isMatch2(s1, s2, idx1 + 1, idx2) || isMatch2(s1, s2, idx1, idx2 + 2); } else{//eg:a b* return isMatch2(s1, s2, idx1, idx2 + 2); } } } else{ if(c2_next != '*'){ return isMatch2(s1, s2, idx1 + 1, idx2 + 1); } else{ return isMatch2(s1, s2, idx1 + 1, idx2) || isMatch2(s1, s2, idx1, idx2 + 2); } } return false; } };
好了,程序由于用的递归,其实思路还算比较清晰。测试了一下,能够击败34%的人。貌似还算一个不错的结果。但是发现大部分人的耗时非常短,所以肯定用了其他方法,而且hint也提示了,用dp。以后在研究。
相关文章推荐
- Java线程间通过共享对象通信
- 自定义控件之仿HD qq的弹出式小菜单
- Android官方提供的支持不同屏幕大小的全部方法
- 输入输出挂
- 质数的无穷性——从素数到数论
- Linux 访问日志统计某个字段并排序功能
- ICP备案的一些tips~
- JBossWeb/Tomcat 初始化连接器和处理 Http 请求过程
- 关于NET编译的目标平台(AnyCpu,x86,x64)
- object-c 混编 调用C,C++接口
- 读书笔记 JavaScript 高级程序设计(第三版)-- 第1章
- Android Audio System 之一:AudioTrack如何与AudioFlinger交换音频数据
- 学习iOS开发前要知道的事儿
- Objective-C和C++混编的要点
- ubuntu 配置安装vsftpd
- iOS开发工具之Alcatraz(Xcode插件管理工具)的安装使用(Xcode7)
- OpenWrt 自学笔记(4)------交叉编译SQLite3
- [转]javascript Date format(js日期格式化)
- 特蕾莎修女的人生信条,以身化道
- 详解iPhone开发之Objective-C和 C 混编