博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode]Wildcard Matching
阅读量:4151 次
发布时间:2019-05-25

本文共 3017 字,大约阅读时间需要 10 分钟。

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() function		if (!*s && !*p) return true;		int ms_max = 1;//size of *s		const 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
> r(2, vector
(ms_max, false));//for the sake of saving space bool flag = 1; r[0][0] = true; do {//*p int 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 character for( ;ms <= ms_max; ++ms) {//up and left if (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];//diagonal r[flag][ms]=r_flag; ++ms;++ss; }while(*ss);//*s r[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');    }};

转载地址:http://ahxti.baihongyu.com/

你可能感兴趣的文章
C#中ColorDialog需点两次确定才会退出的问题
查看>>
数据库
查看>>
nginx反代 499 502 bad gateway 和timeout
查看>>
linux虚拟机安装tar.gz版jdk步骤详解
查看>>
python实现100以内自然数之和,偶数之和
查看>>
去哪儿一面+平安科技二面+hr面+贝贝一面+二面产品面经
查看>>
pytorch
查看>>
pytorch(三)
查看>>
C++ 调用json
查看>>
动态库调动态库
查看>>
Kubernetes集群搭建之CNI-Flanneld部署篇
查看>>
k8s web终端连接工具
查看>>
手绘VS码绘(一):静态图绘制(码绘使用P5.js)
查看>>
手绘VS码绘(二):动态图绘制(码绘使用Processing)
查看>>
基于P5.js的“绘画系统”
查看>>
《达芬奇的人生密码》观后感
查看>>
论文翻译:《一个包容性设计的具体例子:聋人导向可访问性》
查看>>
基于“分形”编写的交互应用
查看>>
《融入动画技术的交互应用》主题博文推荐
查看>>
链睿和家乐福合作推出下一代零售业隐私保护技术
查看>>