10. Regular Expression Matching
来源:互联网 发布:nginx alias用法 编辑:程序博客网 时间:2024/05/20 22:36
一道一开始看题解都看不懂的题,原来是看错题了,*代表某一个字符的0次或者n次,而不是任意字符(一开始就是看错了任意字符,所以看不懂题解)
1. P[i][j] = P[i - 1][j - 1], if p[j - 1] != ‘*’ && (s[i - 1] == p[j - 1] || p[j - 1] == ‘.’);
2. P[i][j] = P[i][j - 2], if p[j - 1] == ‘*’ and the pattern repeats for 0 times;
3. P[i][j] = P[i - 1][j] && (s[i - 1] == p[j - 2] || p[j - 2] == ‘.’), if p[j - 1] == ‘*’ and the pattern repeats for at least 1 times.
好题,要2刷,其实是容易明白的这题,不等于*的时候超级容易懂,就是2,3要看看画图看明白才行。
Ps,注意|| 和&&这些符号的运行顺序,否则容易runwrong。
class Solution {public: bool isMatch(string s, string p) { int n = s.length(); int m = p.length(); vector<vector<bool>>dp(n + 1, vector<bool>(m + 1, false)); dp[0][0] = true; for(int i = 0; i <= n; ++ i) for(int j = 1; j <= m; ++ j){ if(p[j - 1] == '*') dp[i][j] = dp[i][j - 2] || (i > 0 && (s[i - 1] == p[j - 2] || p[j - 2] == '.') && dp[i - 1][j]); else dp[i][j] = i > 0 && dp[i - 1][j - 1] && (s[i - 1] == p[j - 1] || p[j - 1] == '.'); } return dp[n][m]; }};
0 0
- 10. Regular Expression Matching
- 10.Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 《算法竞赛入门经典》第三章思考题
- [蓝牙]蓝牙的初步简介与应用 及其技术要点--更新中
- 目录及文件删除
- HDU5188:zhx and contest(类01背包)
- Android SDK Sample(一) CardReader
- 10. Regular Expression Matching
- 13. Roman to Integer
- 进程状态的转换
- 22. Generate Parentheses
- qt 学习笔记6 360 Labe
- 在SSM中使用shiro实现登录验证(附密码加密)
- 23. Merge k Sorted Lists
- 安卓下Glide缓存问题
- 【ZooKeeper】分布式系统服务器上下线自动感知程序开发