LeetCode_Wildcard Matching
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的更多相关文章
- 学习《Hardware-Efficient Bilateral Filtering for Stereo Matching》一文笔记。
个人收藏了很多香港大学.香港科技大学以及香港中文大学里专门搞图像研究一些博士的个人网站,一般会不定期的浏览他们的作品,最近在看杨庆雄的网点时,发现他又写了一篇双边滤波的文章,并且配有源代码,于是下载下 ...
- LeetCode题解-----Wildcard Matching
题目描述: '?' Matches any single character. '*' Matches any sequence of characters (including the empty ...
- 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 ...
- [LeetCode] Wildcard Matching 外卡匹配
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- [LeetCode] Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- Beginning Scala study note(5) Pattern Matching
The basic functional cornerstones of Scala: immutable data types, passing of functions as parameters ...
- ios 关于问题 no matching provisioning profiles found
ios 关于问题 no matching provisioning profiles found
- iOS 苹果开发证书失效的解决方案(Failed to locate or generate matching signing assets)
从2月14日开始,上传程序的同学可能会遇到提示上传失败的提示. 并且打开自己的钥匙串,发现所有的证书全部都显示此证书签发者无效. 出现以下情况: Failed to locate or generat ...
- 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 检查 ...
随机推荐
- bat命令大全
一.简单批处理内部命令简介 1.Echo 命令 打开回显或关闭请求回显功能,或显示消息.如果没有任何参数,echo 命令将显示当前回显设置. 语法 echo [{on│off}] [message ...
- NSIS脚本调用C语言写的插件
其实NSIS的官网已经提供了很多别人开发的插件了,今天需要用到GetVersion这个插件,这是不维护的插件了,不推荐用,但是由于现实中的问题,导致我不得不用这个插件. 所以就下载下来了. 下载下来之 ...
- openwrt l7过滤qos配置
openwrt l7过滤qos配置 电梯直达 1# 本帖最后由 木鸟 于 2010-7-27 10:22 编辑 openwrt的qos基于hsfc.提供了分类标记,流量控制等功能,可能还有整形 ...
- IDEA 快捷键整理
1. IDEA内存优化 \IntelliJ IDEA 9\bin\idea.exe.vmoptions ----------------------------------------- -Xms6 ...
- 【转】Android开源项目发现---ListView篇(持续更新)
原文网址:http://blog.csdn.net/krislight/article/details/20211045 资料转载地址:https://github.com/Trinea/androi ...
- bzoj1619[Usaco2008 Nov]Guarding the Farm 保卫牧场
Description The farm has many hills upon which Farmer John would like to place guards to ensure the ...
- poj 2976 Dropping tests (二分搜索之最大化平均值之01分数规划)
Description In a certain course, you take n tests. If you get ai out of bi questions correct on test ...
- pyqt之倒计时例子
from PyQt4.Qt import *from PyQt4.QtCore import *from PyQt4.QtGui import *import sysdef main(): a= ...
- C/C++经典面试题目
1.关于动态申请内存 答:内存分配方式三种: (1)从静态存储区域分配:内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.全局变量,static变量. (2)在栈上创建:在执行函数 ...
- sdut2623--The number of steps(概率dp第一弹,求期望)
The number of steps Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 Mary stands in a st ...