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 --------------------------------------------------------------- ...
随机推荐
- android JNI 简单demo(2)它JNI demo 写
android JNI 简单demo(2)它JNI demo 写 一.搭建Cygwin 环境:http://blog.csdn.net/androidolblog/article/details/25 ...
- c# 硬件开源神器netduino的开发中慎用Cpu.Pin
最近为了测试netduino开发板的各个端口是否正常使用,让同事写了一些测试程序,结果出了问题,他的测试程序导致开发板无法发布程序进去,按他的结论是开发板有问题,针对这个情况,我们经过仔细分析代码,认 ...
- 编译的依赖不能vs的release工程
前言: 今天,我们正在做一个ocx插件的时候,放到刚装好win7系统的虚拟机上面注冊,弹出以下的一个错误提示: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ ...
- IIS7.0 Appcmd 命令详解
原文 IIS7.0 Appcmd 命令详解 一:准备工作 APPcmd.exe 位于 C:\Windows\System32\inetsrv 目录 使用 Cd c:\Windows\System32\ ...
- Java加深理解有关注释
一.获取应用笔记 常常会遇到这种情况 package Tokyo.Hot; public class Demo { public static void main(String[] args) { n ...
- 使用nodeitk进行对象识别
前言 东莞,晴,29至27度.忙了一天,最终能够写写东西了.今天继续昨天的话题,我们在昨天的例了基础上完好,通过匹配关键点求出映射从而找到场景中的已知对象. 目标 本文你将学习 採用nodeitk的f ...
- DirectX 9 UI三种设计学习笔记:文章4章Introducing DirectInput+文章5章Wrapping Direct3D
本文从哈利_创.转载请注明出处.有问题欢迎联系本人! 邮箱:2024958085@qq.com 上一期的地址: DX 9 UI设计学习笔记之二 第4章 Introducin ...
- 关于ACM,关于CSU
原文地址:http://tieba.baidu.com/p/2432943599 前言: 即将进入研二,ACM的事情也渐渐远去,记忆终将模糊,但那段奋斗永远让人热血沸腾.开个贴讲讲ACM与中南的故事, ...
- java 不寻常的问题 No bean named 'sessionFactory' is defined 和 initialize a collection of role
左右java的"No bean named 'sessionFactory' is defined " 现在经常出去SHH或在其框架内Sping+JPA使用底部HIbernate ...
- Sharepoint 2013 左右"SPChange"一个简短的引论
于SharePoint于,我们经常需要获得这些更改项目,竟api为我们提供SPChange物.下列,在通过我们的目录资料这一目标. 1.创建测试列表,名字叫做"SPChangeItems&q ...