leetcode 第10题 Regular Expression Matching
来源:互联网 发布:百度地图js touchend 编辑:程序博客网 时间:2024/06/05 20:05
该题主要考正则表达式,需要迭代的去匹配,自然想到用递归,先考虑结束标志位某一串结束或者某一个字符不匹配的情况。针对未结束的中间情况,主要考虑下一位是*与下一位不是*两种情况分别处理,若是*,就需要依次考虑目前字符有0-n个的情况,然后实际处理s+(0,n)与p+2是否匹配。代码如下:
class Solution {
public:
bool isMatch(const char *s, const char *p) {
//cout<<strlen(s)<<" "<<strlen(p)<<endl;
if(p[0]=='\0'){
if(s[0]==p[0])
return true;
else return false;}
if(p[1]!='*'){
if(p[0]==s[0]||(p[0]=='.'&&s[0]))
return isMatch(s+1,p+1);
return false;}
if(p[1]=='*'){
while(s[0]==p[0]||(p[0]=='.'&&s[0])){
if(isMatch(s,p+2))return true;
s++;
}
if(isMatch(s,p+2))return true;
return false;
}
}
};
- 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
- 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
- LeetCode 10: Regular Expression Matching
- leetcode 10 -- Regular Expression Matching
- 自定义Activity的标题栏(Titlebar)
- Zlib数据格式
- [IOS] Xcode中 Search Paths的设置
- SQL Developer 警告--无法安装某些模块
- Android菜单学习
- leetcode 第10题 Regular Expression Matching
- ubuntu 12.04 alt+tab无法切换窗口的问题
- Samples of PL/SQL in JDBC
- LeetCode刷题笔录 Rotate List
- TFS清除不同用户组的工作状态
- volatile关键字
- Linux命令之查找
- python二维码库pyqrcode-0.2.1安装使用
- 批处理