[LeetCode]Wildcard Matching
来源:互联网 发布:javascript游戏编程 编辑:程序博客网 时间:2024/05/05 19:54
class Solution {//1. No match. Simply return false. This is the last case in the program.//2. Single match. Either *s == *p, or *p == '?'. The return value of this case depends on the //result of the rest parts of both s and p (recursion call), which start at the 'diagonal' position by advancing both s and p by 1 (++s, ++p)//3. Star case, i.e. when *p == '*'. This is where the complication comes in. //A star can match 0, 1, 2, ..., to the end of string s. So, as long as one of the substrings match (recursion call), //after advance over '*', it returns true. This case returns false only after exhausting all possible substrings without a match.//update row by row, should practice more, DP, the time complexity is O(n*m)public:bool isMatch(const char *s, const char *p) {// Start typing your C/C++ solution below// DO NOT write int main() functionif (!*s && !*p) return true;int ms_max = 1;//size of *sconst char* ss = s;while(*ss){ ++ms_max; ++ss;}int np_max = 1;const char* pp = p;while(*pp){ if(*pp!='*') ++np_max; ++pp;}if(ms_max < np_max) return false;vector<vector<bool> > r(2, vector<bool>(ms_max, false));//for the sake of saving space bool flag = 1;r[0][0] = true;do{//*pint ms = 1;ss = s;if (*p == '*'){while(*p == '*') ++p;//skip consecutive star--p;r[flag][0] = r[!flag][0];//from the previous row, because star can contain zero characterfor( ;ms <= ms_max; ++ms){//up and leftif (r[!flag][ms] || r[flag][ms-1]) r[flag][ms] = true;else r[flag][ms] = false;}}else{do{//for every character in p, update the row bool r_flag = false;if (*ss == *p || *p == '?')r_flag = r[!flag][ms-1];//diagonalr[flag][ms]=r_flag;++ms;++ss;}while(*ss);//*sr[flag][0] = false;//there must be some character in ss, if current character in p is not star}++p;flag = !flag;//very fantastic}while(*p);return r[!flag][ms_max-1];}};
second time
class Solution {//if strlen is used, then it will be TLE//iteration based solutionpublic: bool isMatch(const char *s, const char *p) { // Start typing your C/C++ solution below // DO NOT write int main() function //int len1 = strlen(s); //int len2 = strlen(p); bool star = false; const char* starPtr = NULL; const char* savePtr = NULL; while(*s != '\0') { if(*p == '?') s++, p++; else if(*p == '*') { while(*p == '*') p++; p--; starPtr = p; p++; savePtr = s; star = true; } else { if(*s == *p) s++, p++; else { if(star == true) s = ++savePtr, p = starPtr+1;//depend on star else return false; } } } while(*p == '*') p++; return (*s == '\0' && *p == '\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
- [LeetCode]Valid Sudoku
- 服务器端开发的一些建议
- [LeetCode]Validate Binary Search Tree
- Asterisk WEBRTC
- haskell(31)
- [LeetCode]Wildcard Matching
- 【OpenGL】使用随机采样实现soft shadow
- 《编程珠玑》第一章第九题
- [LeetCode]Word Ladder
- [LeetCode]Word Ladder II
- 分享:sp_object MYSQL获取当前实例下指定对象与定义语句内容
- PHP 获取域名的方法
- JAVA web服务器底层实现
- [LeetCode]Word Search