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. subTree

    struct Tree() { int val; Tree *left, *right; Tree(int a): val(a), left(NULL), right(NULL){} } bool h ...

  2. dos判断系统版本

    可以通过VER命令的输出结果判断2K/XP/NT,楼主应该对比以下各版本的VER命令输出结果,参考MrPotter(HarryPotter) 的脚本即可, ver|find "XP" ...

  3. struct2(四)编写Struct2 的Action

    简介: 1.映射一个Action到一个类上面 2.把结果返回到view展示 3.编写Action对应的控制逻辑   1. Action Mapping <action name="he ...

  4. C++指针的操作和运算(转)

    既然指针是一种数据类型,那么它也应该有对应的操作或运算,正如整数能做加减乘除一样.但是每一种操作或运算都应该对这种数据类型有意义.比如两个实数可以用关系运算得知哪个大哪个小,而两个虚数却不能使用关系运 ...

  5. FreeBSd ports 安装软件

    1.ports的目录在/usr/ports2.POSTS安装软件有时可能这个包已经安装过了,会有提示,无法 继续安装.能够用提示中的参数:#make install clean FORCE_PKG_R ...

  6. Entify Framewrok - LINQ简单使用

    1.如何使用Join: http://www.devcurry.com/2011/01/join-example-in-linq-and-c.html

  7. DenyHosts限制SSH登录尝试次数

    DenyHosts官方网站为:http://denyhosts.sourceforge.net 1. 安装 # tar -zxvf DenyHosts-2.6.tar.gz # cd DenyHost ...

  8. python高级编程:有用的设计模式3

    # -*- coding: utf-8 -*-__author__ = 'Administrator'#python高级编程:有用的设计模式#访问者:有助于将算法从数据结构中分离出来"&qu ...

  9. javascript:DOM自定义属性的妙用

    虽然HTML元素的属性已经十分丰富,但在某些场合下,元素固有的属性无法完成我们的需求: 这个时候,自定义属性就会让问题解决起来比较方便. 比如,下面的栗子: 多张图片点击变化事件:当背景图片为a.jp ...

  10. The secret of ROWID

    表里每个数据行都有一个行头部,在这里存放了该行数据所包含的列的数量,以及锁定标记等.当某个事务更新某条记录时,会在该数据行的头部记录所用到的ITL槽号以及锁定标记.接下来则是列长度以及列的值.Orac ...