LeetCode #10
来源:互联网 发布:软件系统工程师培训 编辑:程序博客网 时间:2024/06/05 13:17
题目描述:
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
接下来单独处理p长度为1或0情况:①如果p和s都为空,则匹配;②如果p为空而s不为空,则不匹配;③如果s不为空,而p的长度为1,那么s的长度必须为1,且s[0]=p[0],否则不能匹配。
以及s为空的情况:①如果s为空而p不为空,只有在p形如a*b*c*d*....n*的情况下才能匹配,所以p的长度必须为偶数且偶数位元素必须为‘*’,这种情况可以用递归解决。
另外需要注意的是,p[i]='.'时可以等于任何字符。
class Solution {public: bool isMatch(string s, string p) { if(s.size()==0&&p.size()==0) return true; else if(s.size()!=0&&p.size()==0) return false; else if(s.size()==0&&(p.size()%2==0)&&p[1]=='*') return isMatch(s,p.substr(2)); else if(s.size()==0) return false; else if(s.size()!=0&&p.size()!=0) { if(p.size()==1) { if(s.size()==1&&(s[0]==p[0]||p[0]=='.')) return true; else return false; } else if(p[1]!='*') { if(s[0]==p[0]||p[0]=='.') return isMatch(s.substr(1),p.substr(1)); else return false; } else if(p[1]=='*') { if(isMatch(s,p.substr(2))) return true; else if(s[0]==p[0]||p[0]=='.') return isMatch(s.substr(1),p); else if(s[0]!=p[0]&&p[0]!='.') return false; } } }};
递归的方法较为耗时,还可以采用动态规划的方法:①先将两个字符串在首部加上空格字符,便于进行递推。②定义二维数组x[m][n],m、n为s、p的长度,x[i][j]表示s[0]-s[i]和p[0]-p[j]是否匹配,先将二维数组的首行和首列初始化。③进行动态规划,两层循环对数组依次赋值:1、如果p[j]不为‘*’,那么只有p[j]=s[i]且s[0]-s[i-1]和p[0]-p[j-1]匹配,s[0]-s[i]和p[0]-p[j]才能匹配,即x[i][j]为真,否则x[i][j]为假。2、如果p[j]为‘*’,那么如果x[i][j-2]为真则x[i][j]为真,即此时p[j-1]重复0次;如果x[i-1][j]为真,说明s[0]-s[i-1]和p[0]-p[j]匹配,那么只有p[j-1]=s[i]的情况下,s[0]-s[i]和p[0]-p[j]才能匹配;其他的情况x[i][j]都为假。递推结束后返回x[i-1][j-1]就是两个字符串匹配的结果。
class Solution {public: bool isMatch(string s, string p) { s=" "+s; p=" "+p; int m=s.size(); int n=p.size(); bool x[m][n]; x[0][0]=true; for(int i=1;i<m;i++) { x[i][0]=false; } for(int j=1;j<n;j++) { if(p[j]=='*'&&x[0][j-2]==true) { x[0][j]=true; } else x[0][j]=false; } for(int i=1;i<m;i++) { for(int j=1;j<n;j++) { if(p[j]!='*') { if((s[i]==p[j]||p[j]=='.')) x[i][j]=x[i-1][j-1]; else x[i][j]=false; } else if(p[j]=='*') { if(x[i][j-2]==true) x[i][j]=true; else if(x[i-1][j]==true&&(s[i]==p[j-1]||p[j-1]=='.')) x[i][j]=true; else x[i][j]=false; } } } return x[m-1][n-1]; }};
- Leetcode(10)
- LeetCode #10
- leetcode 10: Triangle
- LeetCode(10)RegularExpresssionMatching
- [LeetCode] Valid Palindrome [10]
- leetCode(10):Partition List
- Leetcode--easy系列10
- leetCode练习(10)
- 10 leetcode - Palindrome Number
- Leetcode日记(10)
- LeetCode-10(回文检测)
- leetcode week 10
- leetcode-10-正则表达式
- LeetCode(10)--RegularExpresssionMatching
- [Leetcode] 10, 44, 69
- 4.10 leetcode -10 triangle
- LeetCode刷题(10)
- Leetcode个人题解10
- 如何分叉山寨币开发底层技术并变成私链
- Apache2 Ubuntu Default Page
- python学习心得笔记二
- Python-virtualenv创建虚拟环境
- ajax提交表单
- LeetCode #10
- python3刷博客积分
- SSD 的安裝和配置
- numpy 下的数据结构与数据类型的转换(np.array vs. np.asarray)
- Hadoop Yarn 框架原理及运作机制
- 字符串方法
- gerrit高可用部署
- SpringMVC 404问题解析
- Unicode 和 Utf-8的区别