leetcode题解||Regular Expression Matching 问题 -电脑资料

电脑资料 时间:2019-01-01 我要投稿
【www.unjs.com - 电脑资料】

    problem:

Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the<span>preceding element</span>.The matching should cover the entire input string (not partial).The function prototype should be:bool isMatch(const char *s, const char *p)Some examples:isMatch("aa","a") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "a*") → trueisMatch("aa", ".*") → trueisMatch("ab", ".*") → trueisMatch("aab", "c*a*b") → true

    thinking:

    吐槽几句:

    字符串匹配,‘.‘和‘*‘的意义很简单,不用陈述,但:

isMatch("aab", "c*a*b") → true

    算哪门子事?

    leetcode (http://articles.leetcode.com/2011/09/regular-expression-matching.html)已有好多人在争论,这C*是不是可以代表0个C,我TM无语了,

leetcode题解||Regular Expression Matching 问题

    最后的感觉就是这道题为了宣传那个特定的算法而把条件改了?

    PS: 看错题目了.....2B了

    (1)没有*的情况很好解决,难在怎么处理*

    (2)比如ABBC与 A*C、A*BC,很清楚,利用深搜的思想,只要把BB与*、*B整体作匹配就OK了

    自己写了个测试程序,官方的那个扯淡条件通不过

#include#includeusing namespace std;class Solution {public:    bool isMatch(const char *s, const char *p) {        int n=0;        int m=0;        int index=0;        while(*(s+n)!='\0')        {            n++;        }        while(*(p+m)!='\0')        {            m++;        }       // cout<<"n: "<

    我测试了几组,通过了,欢迎找BUG,

电脑资料

leetcode题解||Regular Expression Matching 问题》(https://www.unjs.com)。

最新文章