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. Unity中的关节

    关节组件一共分为5大类,它们分别是链条关节.固定关节.弹簧关节.角色关节和可配置关节.链条关节(Hinge Joint):将两个物体以链条的形式绑在一起,当力量过大超过链条的固定力矩时,两个物体就会产 ...

  2. Qt浅谈之四十九俄罗斯方块(代码来自网络)

    一.简介 从网上下载了一个Qt实现的俄罗斯方块单机版的源码,觉得非常有意思,故以博客形式记录下来,以便慢慢来研究.在centos6.6下编译运行(注意程序运行需要读取pro目录的配置文件,若把编译目录 ...

  3. Powershell 设置数值格式 1

    设置数值格式 1 6 6月, 2013  在 Powershell tagged 字符串 / 数字 / 文本 / 日期 / 格式化 by Mooser Lee 格式化操作符 -f 可以将数值插入到字符 ...

  4. <php>json小结

    1.页面中如果即用到jquery包,又用到js文件,要先写jquery包,再加载js文件 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tr ...

  5. matlab遗传算法

    function [xv,fv] = myGA(fitness, a, b, NP, NG, Pc, Pm, eps) % 用遗传算法求解一维无约束优化问题 % % 待优化的目标函数 fitness ...

  6. UltraEdit 和Notepad++ 使用ftp直接编辑linux上文件

    安装ftp服务 apt-get install vsftpd 安装完后更改相关配置文件 /etc/vsftpd.conf cp /etc/vsftpd.conf /etc/vsftpd.conf.ol ...

  7. 实战ffs函数

    这个函数是返回整形的最低位1的位置 自己写是这个样子的: /* Find the first bit set in I. */ int lx_ffs(int i) { int index = 0, r ...

  8. Android BLE开发之Android手机与BLE终端通信

    本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处! 近期穿戴设备发展得非常火.把相关技术也带旺了,当中一项是BLE(Bluetooth Low Energy).B ...

  9. poj 1011 Sticks (DFS+剪枝)

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 127771   Accepted: 29926 Descrip ...

  10. core_cm3文件函数一览

    core_cm3是ARM公司推出来的统一规定,这是对下游芯片厂商的统一规定,因此可以再Cortex-M3(CM3)之间进行移植.此文件中定义了一些对特殊功能寄存器的C语言形式的操作,本质上是内敛汇编和 ...