[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次). 另一种是通配符匹配,我们在 ...
随机推荐
- 关于四字节字符入库时错误的解决方案(Incorrect string value: '\xF0\x9F\x99\x8F' for column 'Reply_Content' at row 1)
1. 将表字段字符集设置成utf8mb4 2. 执行插入前执行:SET NAMES utf8mb4; 如: SET NAMES utf8mb4; INSERT test(Content) VALUES ...
- 在不同版本的 IIS 上使用 ASP.NET MVC
ASP.NET MVC Framework 依赖于 URL 路由.为了利用 URL 路由,可能不得不在 Web 服务器上执行额外的配置步骤.这些步骤取决于 Internet Information S ...
- java web-----MVC设计模式
一,MVC将代码分为三个部分,分别为视图(jsp),模型(javaBean),控制部分(servlet); 视图基本为 jsp 文件,主要内容为界面的html代码,负责显示界面: 模型为 javaBe ...
- SQL SERVER删除列,报错."由于一个或多个对象访问此列,ALTER TABLE DROP COLUMN ... 失败"
队友给我修改数据的语句.总是执行失败.很纳闷. 如下图: 仔细看了下这个列,并没有什么特殊.如下图: 但其确实有个约束: 'DF__HIS_DRUG___ALL_I__04E4BC85' . 为什么有 ...
- caffe源码阅读(1)-数据流Blob
Blob是Caffe中层之间数据流通的单位,各个layer之间的数据通过Blob传递.在看Blob源码之前,先看一下CPU和GPU内存之间的数据同步类SyncedMemory:使用GPU运算时,数据要 ...
- Apache(一)
以下介绍是基于centos,Apache/2.2.15,进行解释 (资料来自www.netcraft.com) Apache 模块化的软件 Apache=core+module(内核+模块) core ...
- Redis与Memcached的比较
网络IO模型 Memcached 是多线程,非阻塞IO复用的网络模型,分为监听主线程和worker子线程,监听线程监听网络连接,接受请求后,将连接描述字pipe 传递给worker线程,进行读写IO, ...
- JavaScript 判断用户输入的邮箱及手机格式是否正确
JavaScript判断用户输入的邮箱格式是否正确.判断用户输入的手机号格式是否正确,下面有个不错的示例,感兴趣的朋友可以参考下. 复制代码代码如下: /* * 功能:判断用户输入的邮箱格式是否正确 ...
- li样式不显示使用overflow:hidden导致Li前面点、圈等样式不见
点评:用了overflow:hidden 会影响 list-style,即当ul 中的li 的overflow 为hidden的时候,list-style不起作用,不显示前面的点.圈等样式,在ul或l ...
- Nginx+Keepalived主备负载均衡
实验环境及软件版本: CentOS版本: 6.6(2.6.32.-504.el6.x86_64) nginx版本: nginx-1.6.2 keepalived版本:keepalived ...