http://www.lintcode.com/zh-cn/problem/regular-expression-matching/
class Solution { public: /** * @param s: A string * @param p: A string includes "." and "*" * @return: A boolean */ bool isMatch(const char *s,const char *p) { // write your code here const char*str,*ptr; bool start = false; int flag; int n=0,m =0 ; for (str = s; *str != '\0'; str++)n++; for (ptr = p; *ptr != '\0'; ptr++)m++; n--,m--; while (n>=0) { if (p[m] == s[n] || p[m] == '.')m--,n--; else if(p[m] == '*'){ if (p[m - 1] != s[n]){//s和*前面的不一样 if ( p[m - 1] == '.'){//如果前面是. if (m - 2 >= 0){//如果*前面至少还有2个字符 start = true; flag = m; while (s[n] != p[m - 2]){//如果s有存在.*前面的字符 n--; } } if (m == 1) return true;//如果最后剩下.*就return true; } } else{ while (p[m - 1] == s[n]){ n--; } } m -= 2; } else if (start){ m = flag; } else return false; } while (m>0&& p[m] == '*') m -= 2; return n==-1&&m==-1; } };
原文地址:https://www.jb51.cc/regex/359409.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。