Leetcode-Regular Expression Matching
2015-07-07 13:04
549 查看
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
DP
‘.’ 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
DP
bool isMatch(string s, string p) { int sl=s.length(); int pl=p.length(); vector<vector<bool>> dp(pl+1,vector<bool>(sl+1,false)); dp[0][0]=true; for(int i=1;i<=pl;++i) { dp[i][0]=i-2>=0&&p[i-1]=='*'&&dp[i-2][0]; for(int j=1;j<=sl;++j) { if(p[i-1]==s[j-1]||p[i-1]=='.') { dp[i][j]=dp[i-1][j-1]; } else if(p[i-1]=='*'&&i-2>=0) { if(dp[i-2][j]||((p[i-2]==s[j-1]||p[i-2]=='.')&&dp[i][j-1])) { dp[i][j]=true; } else { dp[i][j]=false; } } else { dp[i][j]=false; } } } return dp[pl][sl]; }
相关文章推荐
- 23. Merge k Sorted Lists
- javascript正则表达式(一)
- 08-nginx网站的配置过程记录
- Reverse Integer - Palindrome Number - 简单模拟
- 理解JS中的prototype
- Unity中的mvc框架_StrangeIoC(之七)
- Unity中的mvc框架_StrangeIoC(之六)
- triplet loss 原理以及梯度推导
- Linux Top 命令解析 比较详细
- 【Java】Java Arrays类进行数组排序
- Unity中的mvc框架_StrangeIoC(之五)
- leetcode 45:Jump Game II
- window coco2dx环境搭建
- 褚时健:年轻人不老想着背靠大树好乘凉
- 保存过高清截图
- ActiveMQ集成到Spring
- iOS接入支付宝(支付功能)
- C语言之基本算法11—牛顿迭代法求平方根
- velocity 各种判断为null或者“null”或者“”总结
- 电脑能ping通外网,解析域名也正常,上不了网?