[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次). 另一种是通配符匹配,我们在 ...
随机推荐
- HW—字符串最后一个单词的长度,单词以空格隔开。
描述 计算字符串最后一个单词的长度,单词以空格隔开. 知识点 字符串,循环 运行时间限制 0M 内存限制 0 输入 一行字符串,长度小于128. 输出 整数N,最后一个单词的长度. 样例输入 hell ...
- Asp.net 定时写入文本记录
Asp.net 定时写入文本记录 public static string FileAddress = "c:\\TimerLog.txt"; protected void Pag ...
- OC4_NSString操作
// // main.m // OC4_NSString操作 // // Created by zhangxueming on 15/6/10. // Copyright (c) 2015年 zhan ...
- 初学dorado
初学dorado 1.dorado使用视图来写界面代码,超级轻松:还需要画流程,页面间的跳转应该很轻松了. 2.先新建dorado项目,再创建dorado视图 3.在Servers里双击tomacat ...
- eNSP
L2交换机 sysvlan 200q interface Vlanif 200ip address 192.168.0.1 24dis thisq interface Ethernet 0/0/1po ...
- Wix installer: suppressing the License Dialog
Reference Link: http://blog.robseder.com/2014/02/20/more-on-wix-and-suppressing-the-license-dialog/ ...
- 存储占用:Memory Map 汉化去广告版
转载说明 本篇文章可能已经更新,最新文章请转:http://www.sollyu.com/storage-occupancy-memory-map-localization-to-billboards ...
- fsockopen/curl/file_get_contents效率比较
前面小节 PHP抓取网络数据的6种常见方法 谈到了 fsockopen,curl与file_get_contents 的使用方法,虽然它们都能达到同一个使用目的,但是它们之间又有什么区别呢? 先谈谈c ...
- bzoj 1096: [ZJOI2007]仓库建设
dp是很好想的了,关键是数据太大,普通dp肯定超时,所以一定有用某种优化,dp优化也就那么几种,这道题用的是斜率优化,先写出普通的状态转移方程: dp[i] = min{ dp[j] + Σ ( p ...
- shell awk
#!/bin/bash # ;i<=;i++)); # do # c1=`shuf -i - -n ` #生成随机数 # c2=`shuf -i - -n ` # c3=`shuf -i - - ...