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)的更多相关文章

  1. [Leetcode][Python]44:Wildcard Matching

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 44:Wildcard Matchinghttps://oj.leetcode ...

  2. 【LeetCode】44. Wildcard Matching (2 solutions)

    Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...

  3. LeetCode: Wildcard Matching 解题报告

    Wildcard MatchingImplement wildcard pattern matching with support for '?' and '*'. '?' Matches any s ...

  4. 【leetcode】Wildcard Matching

    Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...

  5. LeetCode - 44. Wildcard Matching

    44. Wildcard Matching Problem's Link --------------------------------------------------------------- ...

  6. 44. Wildcard Matching

    题目: Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single charact ...

  7. [LeetCode] Wildcard Matching 题解

    6. Wildcard Matching 题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matche ...

  8. Regular Expression Matching & Wildcard Matching

    Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...

  9. [LeetCode][Facebook面试题] 通配符匹配和正则表达式匹配,题 Wildcard Matching

    开篇 通常的匹配分为两类,一种是正则表达式匹配,pattern包含一些关键字,比如'*'的用法是紧跟在pattern的某个字符后,表示这个字符可以出现任意多次(包括0次). 另一种是通配符匹配,我们在 ...

随机推荐

  1. ###Fedora下安装Retext

    使用Markdown. #@date: 2012-05-07 #@author: gr #@email: forgerui@gmail.com 因为习惯了Markdown的简单,所以需要在自己的Fed ...

  2. iOS 的一点理解(一) 代理delegate

    做了一年的iOS,想记录自己对知识点的一点理解. 第一篇,想记录一下iOS中delegate(委托,也有人称作代理)的理解吧. 故名思议,delegate就是代理的含义, 一件事情自己不方便做,然后交 ...

  3. C# ACM poj1007

    求逆序数,快排 public static void acm1007(int a, string[] c) { Dictionary<int, string> dic = new Dict ...

  4. unity访问php

    长连接,弱联网.不好意思,这俩不是一个意思. 反过来说,短连接,强联网,是不是有点别扭呢. 你可以不会php,甚至你可以不知道php是干什么的. 百度php安装环境,自行搭建好环境,顺便测试一下.(下 ...

  5. C#基础(六)——值类型与引用类型

    CLR支持两种类型:值类型和引用类型. 值类型包括C#的基本类型(用关键字int.char.float等来声明),结构(用struct关键字声明的类型),枚举(用enum关键字声明的类型):而引用类型 ...

  6. margin系列之keyword auto

    本系列摘自  px; margin: auto; /* 或者 margin: 0 auto; */ } HTML: <div id="demo"> <p>恩 ...

  7. thinkphp分页实现

    以上为我对于thinkphp分页的实现效果,两种方法,一种调用公共函数中的函数方法(参考http://www.cnblogs.com/tianguook/p/4326613.html),一种是在模型中 ...

  8. 解决laravel中环境配置不起作用的方法

    博客已经迁移到www.imyzf.com,本站不再更新,请谅解! laravel有个环境配置选项很好用,在bootstrap/start.php中,曾经百度到这里面加入域名,就可以自动选择环境 $en ...

  9. python3 中自带urllib库可下载图片到本地

    刚从python3下载图片的语句python2的不太一样,具体python3语句如下: form urllib import request jpg_link = '......'  #图片链接 re ...

  10. MVC-各种传值方式

    [转自]:QLeelulu示例一:ViewData传值.HomeController.cs Co de: public ActionResult Index(){     ViewData[" ...