[OJ] Wildcard Matching (Hard)
LintCode 192. Wildcard Matching (Hard)
LeetCode 44. Wildcard Matching (Hard)
第二次刷还是被这题虐. 其实就是跪在一个地方, 就是关于mustFail的地方.
当*p && !*s的时候, 说明s已经被用完了, p还没有被穷尽, 这种情况下要直接退出所有的递归返回false, 因为s都匹配不完p, 那么s+1, s+2...等字符串更不可能匹配p.
class Solution {
private:
bool mustFail = false;
public:
bool isMatch(const char *s, const char *p) {
if (!s || !p) return false;
while (*s && *p && *s == *p || *p == '?') {
++s;
++p;
}
if (*p == '*') {
while (*p == '*') ++p;
if (!*p) return true;
do {
while (*s && !(*s == *p || *p == '?')) ++s;
if (isMatch(s, p)) return true;
if (mustFail) return false;
++s;
} while (*s);
return false;
}
if (*p && !*s) {
mustFail = true;
}
return !*s && !*p;
}
};
时间复杂度: O(mn)
空间复杂度: O(1)(不考虑递归堆栈开销)
[OJ] Wildcard Matching (Hard)的更多相关文章
- [Leetcode][Python]44:Wildcard Matching
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 44:Wildcard Matchinghttps://oj.leetcode ...
- 【LeetCode】44. Wildcard Matching (2 solutions)
Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...
- LeetCode: Wildcard Matching 解题报告
Wildcard MatchingImplement wildcard pattern matching with support for '?' and '*'. '?' Matches any s ...
- 【leetcode】Wildcard Matching
Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...
- LeetCode - 44. Wildcard Matching
44. Wildcard Matching Problem's Link --------------------------------------------------------------- ...
- 44. Wildcard Matching
题目: Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single charact ...
- [LeetCode] Wildcard Matching 题解
6. Wildcard Matching 题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matche ...
- Regular Expression Matching & Wildcard Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
- [LeetCode][Facebook面试题] 通配符匹配和正则表达式匹配,题 Wildcard Matching
开篇 通常的匹配分为两类,一种是正则表达式匹配,pattern包含一些关键字,比如'*'的用法是紧跟在pattern的某个字符后,表示这个字符可以出现任意多次(包括0次). 另一种是通配符匹配,我们在 ...
随机推荐
- 使用EF访问数据库,出现“System.Data.Entity.Internal.AppConfig”的类型初始值设定项引发异常。
今天在使用的EF时候,发生了"System.Data.Entity.Internal.AppConfig"的类型初始值设定项引发异常.这样的一个错误 查了原因,原来是appconf ...
- __nonnull 和 __nullable (Swift 和 Objective-C混编)
苹果在 Xcode 6.3 以后,为了解决 Swift 与 OC 混编时的问题,引入了一个 Objective-C 的新特性:nullability annotations. 这一新特性的核心是两个新 ...
- ReactiveCocoa 谈谈concat
今天的一个业务流程,业务流程大概就是这样的 1.从CoreData中获取之前的数据 2.更新界面 3.从网络获取数据 4.判断获取结果 5.处理错误判断 6.更新界面 7.判断结果numberOfNe ...
- IO流02_文件过滤器
[简述] 在File类的list方法中可以接收一个FilenameFilter参数,通过该参数可以列出只满足要求的文件. FilenameFilter接口里包含了一个accept(File dir, ...
- 用函数的地址调用函数 C++ MFC
先说一段废话,幸亏我汇编基础(基础 基础 基础)扎实,否则这个问题真的恶心到我了正题 因为有特殊需求, 需要写一个类似于接口的功能,但是因为用到的次数不多再加上C++写接口各种麻烦所以想通过函数的地 ...
- Pascal Language: Recommended Materials
Recommended materials: http://www.marcocantu.com/epascal/
- linux定时执行php脚本
1.查看lynx命令所在的路径 whereis lynx 或者 which lynx 如果没有,则安装 yum install lynx 或者 apt-get方式(ubuntu) 2.建立 shell ...
- IIS tilde directory enumeration 漏洞以及解决方案
2015年6月16日15:19:24 出现 IIS tilde directory enumeration 漏洞 Acunetix Web Vulnerability Scanner 9.5 测试出 ...
- A-Frame 简介03
如果你想开始使用A-Frame可以通过以下几种方式: Play with CodePen Grab the Boilerplate Include the JS Build Install from ...
- gentoo下grub文件编辑
在编译完内核,配置好网络,配置好fstab文件等等,最后一个至关重要的文件要属grub文件了,该文件的配置成功才最终决定gentoo 是否成功装上,首先当然是 emerge grub 了,现在就可以配 ...