leetcode Wildcard Matching
来源:互联网 发布:电视剧《福贵》知乎 编辑:程序博客网 时间:2024/05/18 12:42
先把题目放上来
'?' 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
自己用了两种方法做了这道题,无奈自己实力太差,两次都未能AC,第一次用到的是递归的思路,可是超时了,第二次选择了用动态规划的方法,同样未能AC,主要原因是空间超了,路漫漫其修远兮,吾将上下而求索,以此来勉(qi)励(pian)自己。
上代码:
bool isMatch(const char *s, const char *p) {if (*p == '\0'){return *s == '\0';}if (*s == '\0'){return true;}if (*(p + 1) == '\0'){if (*p == '*'){return true;}else if (*p == '?'){return *(s + 1) == '\0';}else{return *(s + 1) == '\0' && *p == *s;}}if (*p == '?'){return isMatch(s + 1, p + 1);}else if (*p == '*'){for (int i = 0; i < strlen(s); i++){if (isMatch(s + i, p + 1)){return true;}}return false;}else{if (*p != *s){return false;}else{return isMatch(s + 1, p + 1);}}}
动态规划:
bool isMatch(const char *s, const char *p){const int m = strlen(s);const int n = strlen(p);vector<vector<bool>> f(n + 1, vector<bool>(n + 1, false));for (int i = 0; i <= n; i++){f[i][0] = true;}for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++){if (p[i - 1] == '?'){f[i][j] = f[i - 1][j - 1];}else if (p[i - 1] == s[j - 1]){f[i][j] = f[i - 1][j - 1];}else if (p[i - 1] == '*'){if (i != 1){int k;for (k = 1; k <= m; k++){if (f[i - 1][k]){break;}}for (int t = k; t <= m; t++){f[i][t] = true;}break;}else{for (int k = 1; k <= m; k++){f[1][k] = true;}}}else{return false;}}}return f[n][m];}
注意上面两个都是未能AC的。
0 0
- LeetCode: Wildcard Matching
- LeetCode : Wildcard Matching
- [LeetCode] Wildcard Matching - Iteratively
- leetcode 83: Wildcard Matching
- [Leetcode] Wildcard Matching
- [LeetCode]Wildcard Matching
- LeetCode-Wildcard Matching
- LeetCode -- Wildcard Matching
- LeetCode | Wildcard Matching
- Leetcode: Wildcard Matching
- Leetcode:Wildcard Matching
- LeetCode题解:Wildcard Matching
- LeetCode Wildcard Matching
- [Leetcode] Wildcard Matching (Java)
- LeetCode OJ:Wildcard Matching
- Leetcode: Wildcard Matching
- Wildcard Matching -- LeetCode
- [LeetCode] Wildcard Matching
- 10年 阿里
- 【阅读】《Head First JavaScript》第十章——创建自定义对象
- 特殊Hash(概率)
- CoreBluetooth的使用问题汇总
- 商业智能 别人家文章收藏
- leetcode Wildcard Matching
- Linux系统之initramfs工作流程
- A007:文件特殊权限位学习( SUID、SGID、STICKY )
- RailsCasts11 Refactoring User Name Part 2 重构User name(二)
- <mvc:annotation-driven />深度解析
- lua基础
- 二叉树的先序遍历、中序遍历、后续遍历和二叉树还原
- Unity 3D——射线的应用
- 冯·诺依曼结构的主要思想和结构特征