leetcode 第43题 Wildcard Matching
题目:(这题好难。题目意思类似于第十题,只是这里的*就是可以匹配任意长度串,也就是第十题的‘.*’)
'?' 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的更多相关文章
- [LeetCode][Facebook面试题] 通配符匹配和正则表达式匹配,题 Wildcard Matching
开篇 通常的匹配分为两类,一种是正则表达式匹配,pattern包含一些关键字,比如'*'的用法是紧跟在pattern的某个字符后,表示这个字符可以出现任意多次(包括0次). 另一种是通配符匹配,我们在 ...
- [Leetcode][Python]44:Wildcard Matching
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 44:Wildcard Matchinghttps://oj.leetcode ...
- LeetCode(44) Wildcard Matching
题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single characte ...
- [Leetcode 44]通配符匹配Wildcard Matching
[题目] 匹配通配符*,?,DP动态规划,重点是*的两种情况 想象成两个S.P长度的字符串,P匹配S. S中不会出现通配符. [条件] (1)P=null,S=null,TRUE (2)P=null, ...
- 【一天一道LeetCode】#44. Wildcard Matching
一天一道LeetCode系列 (一)题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matches a ...
- 【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 --------------------------------------------------------------- ...
随机推荐
- 一个ajax的Post要求
<1> $.post(url,[data],[callback],[type]) 第一个参数是地址,第二个参数是一个参数传递.第三个参数是一个回调函数.參数是请求返回数据的类型 //一个a ...
- 提高duilib的richedit控制的一些特征
转载请注明原始出处.谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/41208207 假设要使用透明异形窗口功能,首先要改进duilib库让他 ...
- 使用exchange普通表模式被切换到分区表
随着数据库的不断增长的数据量.有些表需要转换的普通堆表分区表模式. 有几种不同的方式来执行此操作,如出口数据表,区表再导入数据到分区表:使用EXCHANGE PARTITION方式来转换为分区表以及使 ...
- PC结束 Spark 二次开发 收到自己主动,并允许好友请求
本次Spark二次开发是为了客服模块的开发, 能让用户一旦点击该客服则直接自己主动加入好友.而客服放则需自己主动加入好友,不同弹出对话框进行允许,这方便的广大客服. 如今废话不多说,直接上代码. pa ...
- WebGL 支持测试,并已支持的浏览器版本摘要
WebGL 支持情况检測与已支持浏览器版本号汇总 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公 ...
- Android定调的发展
首先,介绍一下Android系统支持的铃声格式. 有下面几种: 64赫兹Midi,AAC.AAC+.AMR.WAV.MP3.Real Audio.WMA.OGG等格式. 将音频文件设置成铃声非常eas ...
- Linux Howto
1. Customize the Xfce menu http://wiki.xfce.org/howto/customize-menu
- Android Push Notifications using Google Cloud Messaging (GCM), PHP and MySQL
http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php- ...
- Oracle 用户权限管理方法
Oracle 用户权限管理方法 sys;//系统管理员,拥有最高权限 system;//本地管理员,次高权限 scott;//普通用户,密码默认为tiger,默认未解锁 sys;//系统管理员,拥有最 ...
- Windows移动开发(一)——登堂入室
開始本博客之前先分享一个自己的好消息吧,2014年3月31日起,正式就职于北京****集团Win8project师.主要负责将IOS和Android应用移植到Win8.1平板上,目标客户是银行,闲话不 ...