leetcode Regular Expression Matching
来源:互联网 发布:苹果笔记本推荐 知乎 编辑:程序博客网 时间:2024/06/08 14:54
Implement regular expression matching with support for '.'
and '*'
.
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
这题提示用动态规划,以后回头在用吧
思路:因为要求是全匹配,所有只需要针对. *进程特殊处理
当有。 或者匹配时进行下一个匹配 比如 aa a. ,这里需要注意的是*匹配
0 - n字符,需要先处理下一个是*的情况 ,在处理*时候其实就是针对上一个情况判断是否丢弃
或者说是否针对src进行递推。
class Solution { bool matchstar(const char *src,const char *dst,char c) { do{ if (match(src,dst)) return true; }while(*src != '\0' && (*src++ == c || c == '.')); return false; } bool match(const char *src,const char *dst) { if (dst[0] == '\0' && *src == '\0') return true; if (dst[1] == '*') return matchstar(src,dst+2,dst[0]); if (dst[0] == '\0' || *src == '\0') return false; if ((dst[0] == '.' || dst[0] == *src) && *src != '\0') return match(src+1,dst+1); return false; }public: bool isMatch(string s, string p) { const char *src = s.c_str(); const char *dst = p.c_str(); return match(src,dst); }};
0 0
- 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
- [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
- 多设备时设置default serial的方法
- 嵌入式Web服务器概况
- com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'title' at row 1
- 关于Android读取不同位置(drawable,asset,SDCard)的图片资源的总结
- Js 常用调试的方法
- leetcode Regular Expression Matching
- Ubuntu 重置mysql 密码
- Android-百度地图的调用 (基础版本)
- BeanFactoryPostProcessor和BeanPostProcessor的区别
- 【C语言】字符串操作函数my_strcmp
- jS 回车事件
- cocos2dx 音频模块分析(3): 背景音乐部分
- Step into Scala - 26 - 隐式参数,隐式转换与 DSL
- 得到屏幕大小的两种方法