题目:(这题好难。题目意思类似于第十题,只是这里的*就是可以匹配任意长度串,也就是第十题的‘.*’)
'?' 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. Atitit。团队建设--管理最佳实践--如何留住关键人才,防止人才外流 ??

    Atitit.团队建设--管理最佳实践--怎样留住核心人才,防止人才流失 ?? 1. 1.人才流失后果 1 1. 1.员工的离职带走商业技术秘密和客户等资源 1 2. 2.影响在职员工的情绪.极大挫伤 ...

  2. paip.自适应网页设计 同 响应 与设计的原理的差and实践总结

    paip.自适应网页设计 同 响应 与设计的原理的差and实践总结 响应式Web设计(Responsive Web design)的理念是: 1 #-----------自适应布局VS响应式布局 2 ...

  3. ABP

    ABP ABP之Javascript生成 2015-08-02 18:49 by Barlow Du, 319 阅读, 收藏, 编辑 还是服务在调试SimpleTaskSystem的AngularJs ...

  4. Android学习路线(十一)管理Activity的生命周期

    当一个用户进入.退出,再次进入你的应用时,你的应用中的Activity 会在它的生命周期的各个状态下切换. 比如,当你的activity第一次启动.它出如今系统的前方接受用户的焦点.在这个过程中,An ...

  5. Spring AOP在pointcut expression解析表达式 并匹配多个条件

    Pointcut 方法是那些需要运行"AOP",由"Pointcut Expression"为了描述叙事. Pointcut以下方法可以通过定义任&&a ...

  6. js之面向对象----封装篇

    学习了一天的面向对象总结一下,共分为三类 - -! 老规矩 第一部分是概念性知识!!! 面向对象编程,我们可以把他想象成我们在造人.一个对象便是一个人,这个人有胳膊有腿,这便是一个对象的属性或者方法. ...

  7. Oracle 修改字符集

    出现ORA-12899,是字符集引起的,中文在UTF-8中占3个字节,ZHS16GBK中占2个字节,而源dmp文件字符集是ZHS16GBK库里倒出来的数据,现在要导入到目标字符集为UTF-8的库里,所 ...

  8. 网络资源(3) - iBatis视频

    2018_08_24 http://v.youku.com/v_show/id_XMjk2ODY2OTE2.html iBatis视频教程01

  9. BST(Binary Search Tree)

    原文链接:http://blog.csdn.net/jarily/article/details/8679280 /****************************************** ...

  10. SharePoint 创建一个简单的Web Part 部分

    SharePoint 创建一个简单的Web Part 部分 标准Web零件有时会很强大,运行多个功能的能力. 本文介绍了如何使用Visual Studio 创建一个简单的Web部分. 1. 打开VS, ...