LeetCode
来源:互联网 发布:js 点击图片放大 编辑:程序博客网 时间:2024/06/11 15:02
Implement wildcard pattern matching with support for '?'
and '*'
.
'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).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", "*") → trueisMatch("aa", "a*") → trueisMatch("ab", "?*") → trueisMatch("aab", "c*a*b") → false
写了个递归,然后TLE了,嗯。。于是开始搞dp。
f[i][j]表示 s[0, i) p[0, j) 的匹配
当p[i-1] != '*',f[i][j] = f[i-1][j-1] && (s[i-1] == p[j-1] || p[j-1] == '?')
当p[i-1] == '*',f[i][j] = f[i-1][j] || f[i][j-1]
最开始预处理的时候出了点问题,没处理好一直wa,重点标记一下。
class Solution {public: bool isMatch(string s, string p) { if (p.empty()) return s.empty(); int m = s.length(), n = p.length(); vector<vector<bool> > f(m+1, vector<bool>(n+1, false)); f[0][0] = true; for (int k = 1; k <= n; ++k) { if (p[k-1] != '*') break; f[0][k] = true; } for (int i = 1; i <= m; ++i) { for (int j = 1; j <= n; ++j) { if (p[j-1] != '*') f[i][j] = f[i-1][j-1] && (s[i-1] == p[j-1] || p[j-1] == '?'); else { f[i][j] = f[i-1][j] || f[i][j-1]; } } } return f[m][n]; }};
阅读全文
0 0
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode:
- leetcode
- 网页监控系统设计之使用mjpg-streamer
- MySQL 对于千万级的大表要怎么优化?
- OPC UA--第十四部分 PubSub 发布订阅
- 快速排序
- leetcode 31. Next Permutation
- LeetCode
- SylixOS热插拔驱动接口(三)
- 排序算法之堆排序
- 浏览器兼容性问题
- python request库安装
- 【初探】 归并排序
- 索引动态模板的创建,删除,是否存在
- NOIP模拟题 通讯 强连通分量缩点 最小树形图--朱刘算法
- live555 源码分析:RTSPServer