Implement wildcard pattern matching with support for '?' and '*'.

'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence). 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") ? false
isMatch("aa","aa") ? true
isMatch("aaa","aa") ? false
isMatch("aa", "*") ? true
isMatch("aa", "a*") ? true
isMatch("ab", "?*") ? true
isMatch("aab", "c*a*b") ? false

  方法一: 主要是*的匹配问题。p每遇到一个*,就保留住当前*的坐标和s的坐标,然后s从前往后扫描,如果不成功,则s++,重新扫描。

class Solution {
public:
bool isMatch(const char *s, const char *p) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
bool star = false;
const char *sp, *pp;
for(sp = s, pp = p; *sp != '\0'; sp++,pp++ )
{
switch(*pp)
{
case '?':{
break;
}
case '*':{
star = true;
s = sp;p = pp;
while(*p == '*')p++;
if('\0' == *p) return true;
//* match zero element
sp = s-;
pp = p-;
break;
}
default:{
if(*sp == *pp) break; if(star == true){
sp = s;
pp = p-;
s++;
}else
return false;
}
}
}
while(*pp == '*')pp++;
return *pp == '\0';
}
};

方法二: 递归,不过大数据超时

class Solution {
public:
bool isMatch(const char *s, const char *p) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(*s == '\0' && *p == '\0') return true;
if(*s == '\0'){
while(*p == '*')p++;
return *p == '\0' ;
}
if(*p == '\0') return false; if(*p == '?') return isMatch(s+,p+);
if(*p == '*'){
while(*p == '*')p++;
if(*p == '\0') return true;
return isMatch(s ,p) || isMatch(s+,p) ||
isMatch(s+,p-);
}
return *p == *s && isMatch(s+,p+) ;
}
};

上述代码中: isMatch(s ,p): *匹配了零个元素;isMatch(s+1 ,p) :匹配了一个元素 ;isMatch(s +1,p-1) : 匹配了多个元素

LeetCode_Wildcard Matching的更多相关文章

  1. 学习《Hardware-Efficient Bilateral Filtering for Stereo Matching》一文笔记。

    个人收藏了很多香港大学.香港科技大学以及香港中文大学里专门搞图像研究一些博士的个人网站,一般会不定期的浏览他们的作品,最近在看杨庆雄的网点时,发现他又写了一篇双边滤波的文章,并且配有源代码,于是下载下 ...

  2. LeetCode题解-----Wildcard Matching

    题目描述: '?' Matches any single character. '*' Matches any sequence of characters (including the empty ...

  3. cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:annotation-driven'.

    spring 配置文件报错报错信息:cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be ...

  4. [LeetCode] Wildcard Matching 外卡匹配

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  5. [LeetCode] Regular Expression Matching 正则表达式匹配

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  6. Beginning Scala study note(5) Pattern Matching

    The basic functional cornerstones of Scala: immutable data types, passing of functions as parameters ...

  7. ios 关于问题 no matching provisioning profiles found

    ios 关于问题 no matching provisioning profiles found

  8. iOS 苹果开发证书失效的解决方案(Failed to locate or generate matching signing assets)

    从2月14日开始,上传程序的同学可能会遇到提示上传失败的提示. 并且打开自己的钥匙串,发现所有的证书全部都显示此证书签发者无效. 出现以下情况: Failed to locate or generat ...

  9. ORA-12516:TNS:listener could not find available handler with matching protocol stack

    应用程序连接测试数据库时报ORA-12516:TNS:listener could not find available handler with matching protocol stack 检查 ...

随机推荐

  1. Powershell过滤管道结果

    通过管道可以过滤某些对象和对象的属性,这个功能很实用,因为很多时候我们并不是对所有的结果感兴趣,可能只会对某些结果感兴趣.如果要过滤对象可以使用Where-Object:如果要过滤对象的属性,可以使用 ...

  2. hdu5072(鞍山regional problem C):容斥,同色三角形模型

    现场过的第四多的题..当时没什么想法,回来学了下容斥,又听学长讲了一讲,终于把它过了 题目大意:给定n个数,求全部互质或者全部不互质的三元组的个数 先说一下同色三角形模型 n个点 每两个点连一条边(可 ...

  3. Squid--hash代码分析

    #ifndef SQUID_HASH_H #define SQUID_HASH_H //几个函数和变量的别名 typedef void HASHFREE(void *); typedef int HA ...

  4. struts的ognl.NoConversionPossible错误

    JSP页面便利集合的时候,代码如下 <s:iterator value="storageList" id="stList" status="st ...

  5. testng xml 示例

    TestNG的DTD检查文件:http://testng.org/testng-1.0.dtd.php 更多testng配置及说明,请移步http://testdoc.org/docmaster?pi ...

  6. php之手机号码查归属地

    免费手机号码归属地API查询接口 一.淘宝网API API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=1585078144 ...

  7. ipython with ubuntu

    在Linux环境下,其实IDE环境配置比较容易配.所以建议用linux做开发. 首选启动终端:Ctrl+Alt+T sudo apt-get update sudo apt-get install p ...

  8. Javascript:字符串分割split()妙用

    概述: split() 方法将字符串分割为字符串数组,并返回此数组 语法格式: stringObject.split(separator,limit) 参数说明: 注意:如果把空字符串 (" ...

  9. ashx实现文件下载以及文件MD5码测试

    cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System ...

  10. resin安装和配置

    1 从 http://www.caucho.com/download/ 下载resin 2 检查你的linux环境,查看是否安装了 jdk1.5 或以上版本,是否安装了perl. 输入命令:Java ...