【LeetCode】44. Wildcard Matching (2 solutions)
Wildcard Matching
Implement wildcard pattern matching with support for '?' and '*'.
'?' 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
解法一:
这题想法参考了https://oj.leetcode.com/discuss/10133/linear-runtime-and-constant-space-solution,
类似于一种有限状态机的做法。
主要思想如下:
由于*匹配多少个字符是不一定的,于是首先假设*不匹配任何字符。
当后续匹配过程中出错,采用回溯的方法,假设*匹配0个字符、1个字符、2个字符……i个字符,然后继续匹配。
因此s需要有一个spos指针,记录当p中的*匹配i个字符后,s的重新开始位置。
p也需要一个starpos指针指向*的位置,当回溯过后重新开始时,从starpos的下一位开始。
class Solution {
public:
bool isMatch(const char *s, const char *p) {
char* starpos = NULL;
char* spos = NULL;
while(true)
{
if(*s == && *p == )
//match all
return true;
else if(*s == && *p != )
{//successive *p must be all '*'
while(*p != )
{
if(*p != '*')
return false;
p ++;
}
return true;
}
else if(*s != && *p == )
{
if(starpos != NULL)
{//maybe '*' matches too few chars in s
p = starpos + ;
s = spos + ;
spos ++; //let '*' matches one more chars in s
}
else
//*s is too long
return false;
}
else
{
if(*p == '?' || *s == *p)
{
s ++;
p ++;
}
else if(*p == '*')
{
starpos = (char*)p;
spos = (char*)s;
p ++;
//start successive matching from "'*' matches non"
}
//currently not match
else if(starpos != NULL)
{//maybe '*' matches too few chars in s
p = starpos + ;
s = spos + ;
spos ++; //let '*' matches one more chars in s
}
else
//not match
return false;
}
}
}
};

解法二:
模仿Regular Expression Matching的递归做法,小数据集上能过。
当*数目太多时会超时。
class Solution {
public:
bool isMatch(const char *s, const char *p) {
if(*p == )
return (*s == );
if(*p == '*')
{
//case1: *p matches 0 chars in s
if(isMatch(s, p+))
return true;
//case2: try all possible numbers
while(*s != )
{
s ++;
if(isMatch(s, p+))
return true;
}
return false;
}
else
{
if((*s==*p) || (*p=='?'))
return isMatch(s+, p+);
else
return false;
}
}
};
以下是我的测试代码,小数据上全部通过:
int main()
{
Solution s;
cout << s.isMatch("aa","a") << endl;
cout << s.isMatch("aa","aa") << endl;
cout << s.isMatch("aaa","aa") << endl;
cout << s.isMatch("aa","*") << endl;
cout << s.isMatch("aa","a*") << endl;
cout << s.isMatch("ab","?*") << endl;
cout << s.isMatch("aab","c*a*b") << endl;
return ;
}
【LeetCode】44. Wildcard Matching (2 solutions)的更多相关文章
- 【leetcode】44. Wildcard Matching
题目如下: 解题思路:本题和[leetcode]97. Interleaving String非常相似,同样可以采用动态规划的方法.记dp[i][j] = 1或者0 表示pattern[0:i]是否匹 ...
- 【一天一道LeetCode】#44. Wildcard Matching
一天一道LeetCode系列 (一)题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matches a ...
- 【leetcode】Regular Expression Matching (hard) ★
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【LeetCode】75. Sort Colors (3 solutions)
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 【LeetCode】90. Subsets II (2 solutions)
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 【leetcode】Regular Expression Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
- 【LeetCode】28. Implement strStr() (2 solutions)
Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haysta ...
- 【LeetCode】130. Surrounded Regions (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- 【LeetCode】1023. Camelcase Matching 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则+字典 日期 题目地址:https://leet ...
随机推荐
- python类和实例以及__call__/__del__
面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各自的数据可 ...
- Snail—Hibernate反向生成实体类及配置文件
今天学习了Hibernate的反向生成类文件 第一步.打开myeclipse中的database视图,找到对应的表,选中后右键单击. watermark/2/text/aHR0cDovL2Jsb2cu ...
- request和request.form和request.querystring的区别
asp中获取传递的参数,一般用request或者用request成员函数request.form,两种方式都可以获取页面表单传递过来的参数值,一直没留意两种方法有什么区别,我一般喜欢用request( ...
- 关于VS 工具箱灰色,不可用的解决方案
使用vs的命令行工具,在命令行中运行:devenv /ResetSkipPkgs ,重新打开vs,重置一下工具箱 ,OK,成功了~! 希望能对大家有帮助!
- 【系统】supervisor支持多进程
[program:deployworker] directory = /etc/ansible/easyAnsible/app/deploy/ command = python Deploy.py p ...
- 简单账本-用完即走的微信小程序
作为一个记账强迫症患者,对当前手机中的记账App都不太满意.这类软件越来越臃肿,越来越慢,启动要半天.联网同步要半天,进入界面又有一堆新功能要介绍.好不容易开始记账,又得各种高大上的选择设定,一笔帐下 ...
- 用Java发送HTML格式邮件测试类(支持中文)
代码由纯Java写成,支持中文,一目了然,只要将Main函数中的相关信息填写正确就直接用了,便于修改,可以在此类基础上任意扩展成自己的类. 注意做HTML形式的邮件,最好把HTML,CSS都写全,只写 ...
- 5种调优Java NIO和NIO.2的方式
Java NIO(New Input/Output)——新的输入/输出API包——是2002年引入到J2SE 1.4里的.Java NIO的目标是提高Java平台上的I/O密集型任务的性能.过了十年, ...
- Linux服务启动出现java.net.UnknownHostException:
第一种情况: 一般是在/etc/hosts文件,在文件最后加入这句配置,重启服务即可 172.xx.xxx.xxx localhost.localdomain xxx-houtai1 第二种情况: ...
- Proguard随笔
- ProGuard是一个压缩.优化和混淆Java字节码,它能够删除字节码中无用的类.字段.方法和无用的凝视,还能够对类.字段.方法和属性进行混淆. - 字节码事实上包括了大量的调试信息,从而非常ea ...