题目:(这题好难。题目意思类似于第十题,只是这里的*就是可以匹配任意长度串,也就是第十题的‘.*’)
'?' 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
题目的意思就不重复了如第十题,就是匹配的问题,这里的'?'相当于第十题的'.',匹配单一字符,这里的'*'相当如第十题的'.*'匹配任意长度的字符。所以是不是就想到可以用相同的递归方法就好了。
不行的,会超时,也算是熟悉了下递归思路吧。超时代码如下:
 class Solution {
public:
bool isMatch(const char *s, const char *p)
{
while(p + != NULL && *(p+) == *p && *p == '*') p++;
if (*p == '\0')
return *s == '\0';
if (*s == *p || *p == '?' && *s != '\0')
return isMatch(s + , p + );
else if (*p == '*')
{
if (*s == '\0')
return isMatch(s, p + );
while(*s != '\0')
{
if(isMatch(s, p + )) return true;
s++;
}
}
return false;
}
};

其实这个动态规划也不那么好理解。看了半天才把这个人的代码看懂。我加了注释如下:

主要就是要弄清楚下标。他的下标有点乱,但又不得不那样。dp[lens + 1][lenp + 1]是因为还要存长度为0 和0的匹配,以及0和若干个*的匹配都是真值。

dp[j][i]的意思是,s的第1到j和p的第1到i是否匹配,也就是下标s的0到j-1的字符串和p的下标0到i-1的字符串是否匹配。

则:

if p[j] == '*' && (dp[i][j-1] || dp[i-1][j])  ------a

dp[i][j] =  1

else p[j] = ? || p[j] == s[i]     -------b

dp[i][j] = dp[i-1][j-1];

else dp[i][j] = false;     --------c

class Solution {
public: bool isMatch(const char *s, const char *p) {
int len_s = strlen(s);
int len_p = strlen(p); const char* tmp = p;
int cnt = ;
while (*tmp != '\0') if (*(tmp++) != '*') cnt++;
if (cnt > len_s) return false;// 如果没有*的长度比待匹配的字符串还长是不可能匹配的,所以false bool dp[len_s + ][len_p + ];
memset(dp, false,sizeof(dp)); dp[][] = true;
for (int i = ; i <= len_p; i++) {
if (dp[][i-] && p[i-] == '*') dp[][i] = true; // p[i-1]是p的第i个,dp[][i-1]是到p的第i-1个
//所以如果p的前i-1个是*,并且第i个也是*的话,那么dp[0][i]就是真
for (int j = ; j <= len_s; ++j)
{
if (p[i-] == '*') dp[j][i] = (dp[j-][i] || dp[j][i-]); //注意下标就发现,对应上面博文中的注释------a
else if (p[i-] == '?' || p[i-] == s[j-]) dp[j][i] = dp[j-][i-]; //对应博文中的------b
else dp[j][i] = false; //对应博文中的-------c
}
}
return dp[len_s][len_p];
}
};

还有一个是据说80ms过的。用贪心的。贴出如下:

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 && !p) return true; const char *star_p=NULL,*star_s=NULL; while(*s)
{
if(*p == '?' || *p == *s)
{
++p,++s;
}else if(*p == '*')
{
//skip all continuous '*'
while(*p == '*') ++p; if(!*p) return true; //if end with '*', its match. star_p = p; //store '*' pos for string and pattern
star_s = s;
}else if((!*p || *p != *s) && star_p)
{
s = ++star_s; //skip non-match char of string, regard it matched in '*'
p = star_p; //pattern backtrace to later char of '*'
}else
return false;
} //check if later part of p are all '*'
while(*p)
if(*p++ != '*')
return false; return true;
}
};

最后我想说,真TM难。。。

leetcode 第43题 Wildcard Matching的更多相关文章

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

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

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

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

  3. LeetCode(44) Wildcard Matching

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

  4. [Leetcode 44]通配符匹配Wildcard Matching

    [题目] 匹配通配符*,?,DP动态规划,重点是*的两种情况 想象成两个S.P长度的字符串,P匹配S. S中不会出现通配符. [条件] (1)P=null,S=null,TRUE (2)P=null, ...

  5. 【一天一道LeetCode】#44. Wildcard Matching

    一天一道LeetCode系列 (一)题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matches a ...

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

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

  7. LeetCode: Wildcard Matching 解题报告

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

  8. 【leetcode】Wildcard Matching

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

  9. LeetCode - 44. Wildcard Matching

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

随机推荐

  1. python使用smtplib库和smtp.qq.com邮件服务器发送邮件(转)

    使用qq的邮件服务器需要注意的两个地方主要是: 1.协议问题 使用465端口 SSL 协议 2.口令问题 出现SMTPAuthenticationError 主要的原因就是口令和帐号信息不对,这里我们 ...

  2. iOS_21团购_发送请求【点评】数据

    结果表明,一个简单的请求: 用到的点评封装的类: 使用tableView简单展示: // // DealListController.m // 帅哥_团购 // // Created by beyon ...

  3. Cocos2d-x 2.2.3 Android配置

    今天总结出来的部署流程,已经成功把自己的项目编译到android真机上.省去了安装ndk等步骤 环境: win7 64位 1.导入项目到eclipse 2.导入libcocos2dx 样例:C:\co ...

  4. java 它 引用(基本类型的包装,构造函数和析构函数c++不同)

      一个:java 和c++参考控制 他提到引用,我们会想到java它不喜欢c++里面的指针.当然java内引用和c++里面的引用是不同的. 比如: 比方C++中,我对某一个函数的声明.int a(i ...

  5. cocos2dx 制作单机麻将(二)

    cocos2dx 制作单机麻将(二) 打乱麻将顺序2 前面解说了怎样打乱初始给定的麻将牌堆, 另一种是打乱随意给定的麻将牌堆 //混乱扑克2 void RandAppointCardData(BYTE ...

  6. URAL 1141. RSA Attack RSA加密演算法

    标题来源:URAL 1141. RSA Attack 意甲冠军:给你e n c 并有m^e = c(mod n) 求 m 思路:首先学习RSA算法 here 过程大致是 1.发送的信息是m 2.随机选 ...

  7. java求最大公约数(分解质因数)

    下面是四种用java语言编程实现的求最大公约数的方法: package gcd; import java.util.ArrayList; import java.util.List; public c ...

  8. (大数据工程师学习路径)第一步 Linux 基础入门----Linux 下软件安装

    介绍 介绍 Ubuntu 下软件安装的几种方式,及 apt,dpkg 工具的使用. 一.Linux 上的软件安装 通常 Linux 上的软件安装主要有三种方式: 在线安装 从磁盘安装deb软件包 从二 ...

  9. 记录这一刻:百度搜索结果“文件格式:-HTML文本”

    只要百度搜索关键词无论结果是"文件格式:-HTML文本",现在,这个问题已经被修复. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG ...

  10. hdu4699 Editor 2013 多校训练第十场 D题 数列维护 splay | 线段树 | 栈!!!!!

    题意:维护一个文本编辑,并且查询最大前缀和. 写了splay,wa了13次 过了之后觉着特傻逼.发现题解两个栈就可以了,光标前后维护两个栈,维护前面的栈的前缀和 和 最大前缀和. 哎,傻逼,太弱了,还 ...