leetcode 10 -- Regular Expression Matching
2015-06-24 00:38
459 查看
前言
初看到这道题,毫无思路,逃避好几天,看学长博客,才找到思路,即使这样,仍调试好大一会儿,刷题好痛苦。。。
题目
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”) → 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
题意
完成类似正则表达式的匹配功能
思路
针对p[1]是否等于*来进行递归滑动
代码
初看到这道题,毫无思路,逃避好几天,看学长博客,才找到思路,即使这样,仍调试好大一会儿,刷题好痛苦。。。
题目
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”) → 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
题意
完成类似正则表达式的匹配功能
思路
针对p[1]是否等于*来进行递归滑动
代码
bool isMatch(char *s,char *p) { if(p[0]=='\0') { return s[0]=='\0'; } if(s[0]=='\0') { if(p[1]=='*') { return isMatch(s,p+2); } return false; } if(p[1]=='*') { if(s[0]==p[0]||p[0]=='.') { if(strlen(p)>=4 && p[0]==p[2] && p[3]=='*') return isMatch(s,p+2);//此处总觉得是有些取巧,针对超时 return isMatch(s+1,p)||isMatch(s+1,p+2)||isMatch(s,p+2); } else { return isMatch(s,p+2); } } else { if(s[0]==p[0]||p[0]=='.') { return isMatch(s+1,p+1); } } return false; }
相关文章推荐
- 获取文件夹下得所有文件,并按文件夹先,文件后的顺序排
- LeetCode——Happy Number
- IOS 开发注意的问题
- volley源码解析(七)--最终目的之Response<T>
- 看锋利的Jquery发现有感
- 轮廓检测(1)
- 触摸事件
- OBJ文件格式详解
- android调试时apk可运行,导出签名的apk后闪退
- 笔记-Microsoft SQL Server 2008技术内幕:T-SQL语言基础-09 事务和并发
- synchronized使用方法
- 隐藏状态栏、设置全屏、取消全屏
- Java中Synchronized的用法
- 隐藏状态栏、设置全屏、取消全屏 2015-06-24 00:25 7人阅读 评论(0) 收藏
- 内存管理
- hadoop图像处理接口hipi
- 怪异问题:qwidget: must construct a qapplication before a qpaintdevice
- hadoop的文件操作
- Functional Programming in Scala第二章练习
- 文件的创建,读取,写入,修改,删除