題解:正則表達式
這道題沒法使用動態規劃,由於不知道字符的長度;函數
嗯嗯,正則匹配我仍是稀裏糊塗 的,等弄明白了,再補充 ~待更新。。。spa
1 class Solution { 2 public: 3 bool match(char* str, char* pattern) 4 { 5 if (str == nullptr || pattern == nullptr)return false; 6 return matchCore(str, pattern); 7 } 8 bool matchCore(const char *str, const char *pattern) 9 { 10 if (*str == '\0' && *pattern == '\0')return true; 11 if (*str != '\0'&& *pattern == '\0')return false; 12 if (*(pattern + 1) == '*') 13 { 14 if (*pattern == *str || (*pattern == '.' && *str != '\0')) 15 return matchCore(str + 1, pattern + 2) || //移動到下一個位置 16 matchCore(str + 1, pattern) || // 待在本身的位置不動 17 matchCore(str, pattern + 2);//忽略一個‘*’ 18 else 19 return matchCore(str, pattern + 2);//忽略一個'*' 20 } 21 if (*str == *pattern || (*pattern == '.' && *str != '\0')) 22 return matchCore(str + 1, pattern + 1); 23 return false; 24 } 25 };