LeetCode OJ Wildcard Matching
来源:互联网 发布:微信淘口令淘宝打不开 编辑:程序博客网 时间:2024/04/28 03:09
https://oj.leetcode.com/discuss/10133/linear-runtime-and-constant-space-solution
class Solution {public: bool isMatch(const char * s, const char * p) { int posOfStar = -1; // p中最近一次*号的位置 int posOfTail = 0; // 在s中被p中用*号代替的子字符串的串尾的下一位 int si = 0; int pi = 0; while (s[si] != '\0') { //如果匹配上了都往下一位 if (p[pi] == '?' || p[pi] == s[si]) { pi++; si++; continue; } //如果p中出现了*,第一更新posOfStar,说明有*号可以在不得已的时候使用(也就是下一步) if (p[pi] == '*') { posOfStar = pi; pi++; // 同时pi往下移动准备下次匹配 posOfTail = si; // 这个时候说明被*号代替的子字符串长度为0 continue; } //如果这个时候既不匹配,pi位置上也没有*,说明走投无路了,为了尽量匹配只好用之前出现过的*来代替s中不匹配的内容 if (posOfStar != -1) { pi = posOfStar + 1; // 因此pi位置要重新返回到*号的下一位准备上述代替后的匹配 posOfTail++; // 那么s中被p中用*号代替的子字符串增加一位 si = posOfTail; // 同时下一次si的起始点也后移,后移到在s中被p中用*号代替的子字符串的串尾的下一位,也就是下一次匹配的开始位 continue; } //如果运行到这一步说明没有任何办法匹配了 return false; } while (p[pi] == '*') pi++; // 因为上述循环在s用完的时候就结束了,我们要检查p中的剩余元素,*可以代表没元素,因此可跳过 return p[pi] == '\0'; // 如果p中还有剩余就不匹配了 }};
0 0
- LeetCode OJ:Wildcard Matching
- LeetCode OJ - Wildcard Matching
- LeetCode OJ Wildcard Matching
- leetCode OJ 44. Wildcard Matching
- LeetCode OJ算法题(四十三):Wildcard Matching
- LeetCode OJ 之 Wildcard Matching(通配符匹配)
- 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 OJ Substring with Concatenation of All Words
- SSH 之 Spring的源码(一)——Bean加载过程
- MFC控件:listctrl使用方法总结
- maven打包报错:Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.5:test
- C++类和动态内存分配
- LeetCode OJ Wildcard Matching
- UVa 11488 Hyper Prefix Sets(字典树)
- LeetCode OJ Permutations II
- OC学习小结之protocol
- LeetCode OJ Jump Game II
- 当初我未嫁,你未娶。你干什么去了?
- LeetCode OJ Permutations
- LeetCode OJ Combination Sum II
- LeetCode OJ Combination Sum