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 检查 ...
随机推荐
- Codeforces 573B Bear and Blocks
http://codeforces.com/problemset/problem/573/B 题目大意: 给出n个连续塔,每个塔有高度hi,每次取走最外层的块,问需要多少次操作能够拿光所有的块. 思 ...
- Qt编程之通过鼠标滚轮事件缩放QGraphicsView里面的Item
首先自己subclass QGraphicsView的一个类,叫DiagramView,然后重新实现它的滚轮事件函数,然后发送一个缩放信号: oid DiagramView::wheelEvent(Q ...
- js深入研究之牛逼的类封装设计
<script type="text/javascript"> var Book = function(newIsbn, newTitle, newAuthor) { ...
- 理解Spring MVC Model Attribute和Session Attribute
作为一名 Java Web 应用开发者,你已经快速学习了 request(HttpServletRequest)和 session(HttpSession)作用域.在设计和构建 Java Web 应用 ...
- AngularJs学习笔记4——四大特性之双向数据绑定
双向数据绑定 方向1:模型数据(model)绑定到视图(view) 实现方法:①.{{model变量名}} ②.常用指令(ng-repeat) 方向2:将视图(view)中用户输入的数据绑定到模型数 ...
- Android: 在WebView中获取网页源码
1. 使能javascript: ? 1 webView.getSettings().setJavaScriptEnabled(true); 2. 编写本地接口 ? 1 2 3 4 5 final c ...
- 502 bad gateway是什么意思
通俗解释一下 1.什么是502 bad gateway 报错? 简单来说 502 是报错类型代码 bad gateway 错误的网关 2.产生错误的原因 连接超时 我们向server器发送请求 因为s ...
- 退役笔记一#MySQL = lambda sql : sql + ' Source Code 4 Explain Plan '
Mysql 查询运行过程 大致分为4个阶段吧: 语法分析(sql_parse.cc<词法分析, 语法分析, 语义检查 >) >>sql_resolver.cc # JOIN.p ...
- php不会的点
1.DIRECTORY_SEPARATOR:DIRECTORY_SEPARATOR是一个显示系统分隔符的命令,DIRECTORY_SEPARATOR是PHP的内部常量,不需要任何定义与包含即可直接使用 ...
- indexOf()忽略大小写方法
indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置.如果没有出现,则输出-1. indexOf() 方法对大小写敏感!所以要检索字符串且忽略大小写的时候,可以先把字符串转换成全部 ...