leetcode(10) - Regular Expression Matching 正则表达式匹配
发布时间:2020-12-14 04:17:17 所属栏目:百科 来源:网络整理
导读:. 表示匹配任意字符 * 表示匹配0个或多个字符 bool isMatch(char* s,char* p) { if (*p == ' ') return *s == ' '; // next char is not '*',then must match current character if (*(p + 1) != '*') { //如果下一个字符是*, 则当前字符应相等,或者当前
. 表示匹配任意字符 * 表示匹配0个或多个字符
bool isMatch(char* s,char* p) { if (*p == ' ') return *s == ' '; // next char is not '*',then must match current character if (*(p + 1) != '*') { //如果下一个字符是*, 则当前字符应相等,或者当前字符是. if (*p == *s || (*p == '.' && *s != ' ')) return isMatch(s + 1,p + 1); else return false; } else { // next char is '*' while (*p == *s || (*p == '.' && *s != ' ')) { // 过滤s中当前匹配已经确定匹配的位置,寻找下一个s需要去匹配的位置。p的位置不动。(1)如果p是字符,则是“字符*”, ,应判断是否(*p==*s),s++ (2)如果p是.,则".*”可以匹配任意多个字符,应调用isMatch if (isMatch(s,p + 2)) // *p=='.'时: 如果这步一直是false,通过s++找到s接下来需要去匹配的位置 return true; s++; } return isMatch(s,p + 2); // p+2是*的下一个字符 } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |