【一天一道LeetCode】#44. Wildcard Matching
一天一道LeetCode系列
(一)题目
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
(二)解题
1、递归解法
看到这题首先想到的是之前做的正则表达式那题【一天一道LeetCode】#10. Regular Expression Matching,于是想都没有想,按照之前的方法,很明显,超时了。
/*
1.如果s[i]==p[j]||p[j] == '?' ,则i++,j++
2.如果p[j] =='*',就判断s[i]和p[j+1]以后的字串能否匹配上,如果能则返回true,如果不能则i++
*/
class Solution {
public:
bool isMatch(string s, string p) {
int i = 0;
int j = 0;
while(i<s.length()&&j<p.length())
{
if(s[i] == p[j] || p[j] == '?')
{
i++;
j++;
}
else if(p[j] == '*')
{
if(j==p.length()-1) return true;
else
{
while(i<s.length()&&s[i]!=p[j+1]) i++;
if(i==s.length()) return false;
else{
string tmps = s.substr(i,s.length());
string tmpp = p.substr(j+1,p.length());
if(isMatch(tmps,tmpp)) return true;//判断后面的能否匹配
else i++;
}
}
}
else break;
}
if(i==s.length()&&j==p.length()) return true;
else return false;
}
};
2、 回溯法
首先我们看一个例子caabbbbc和c*ab*c,后者可以写成c……ab…..c,这样一来我们只要在两个c之间找到ab就能匹配上了,
于是当碰到*的时候就记录下此时的spre = i和ppre = ++j,然后比较s[++i]和p[++j],如果不等就回溯到i=++spre,j=ppre ,
如果碰到下一个‘*’ , 就代表ab已经匹配完成了,更新spre和ppre,循环比较,直到字符串尾。
这就是本解法主要思想。
class Solution {
public:
bool isMatch(string s, string p) {
int i = 0,j = 0;
int slen = s.length();
int plen = p.length();
int spre = 0 , ppre = 0;//回溯法
bool isflag = false;
while(i<slen)
{
if(s[i]==p[j] || p[j]=='?')
{
i++;
j++;
}
else if(p[j] == '*')
{
ppre = ++j;//记录*位置,一遍回溯匹配
spre = i;
isflag = true;//代表前面出现过‘*’
}
else
{
if(isflag)//s[i] != p[j]而且p[j]!='?',匹配失败,回溯
{
i = ++spre;
j = ppre;
}
else return false;//如果前面没有‘*’,则代表匹配失败,返回false
}
}
while(p[j]=='*') j++;//防止出现p末尾都是‘*’的特殊情况
if(j==plen) return true;
else return false;
}
};
【一天一道LeetCode】#44. Wildcard Matching的更多相关文章
- LeetCode - 44. Wildcard Matching
44. Wildcard Matching Problem's Link --------------------------------------------------------------- ...
- 第八周 Leetcode 44. Wildcard Matching 水题 (HARD)
Leetcode 44 实现一种类似正则表达式的字符串匹配功能. 复杂度要求不高, 调代码稍微费点劲.. 好像跟贪心也不太沾边, 总之 *把待匹配串分成若干个子串, 每一个子串尽量在模式串中靠前的部分 ...
- [LeetCode] 44. Wildcard Matching 外卡匹配
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...
- [leetcode]44. Wildcard Matching万能符匹配
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...
- LeetCode 44 Wildcard Matching(字符串匹配问题)
题目链接:https://leetcode.com/problems/wildcard-matching/?tab=Description '?' Matches any single chara ...
- leetcode 44. Wildcard Matching(模糊匹配)
搬运工了- - https://blog.csdn.net/jmspan/article/details/51460021
- leetcode 10. Regular Expression Matching 、44. Wildcard Matching
10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...
- 44. Wildcard Matching
题目: Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single charact ...
- 【LeetCode】44. Wildcard Matching (2 solutions)
Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...
随机推荐
- 基于Python预测股价
▌实现预测的Stocker工具 Stocker是一款用于探索股票情况的Python工具.一旦我们安装了所需的库(查看文档),我们可以在脚本的同一文件夹中启动一个Jupyter Notebook,并导入 ...
- 什么是 Docker
Docker 是一个开源项目,诞生于 2013 年初,最初是 dotCloud 公司内部的一个业余项目.它基于 Google 公司推出的 Go 语言实现. 项目后来加入了 Linux 基金会,遵从了 ...
- Linux系统网络性能实例分析
由于TCP/IP是使用最普遍的Internet协议,下面只集中讨论TCP/IP 栈和以太网(Ethernet).术语 LinuxTCP/IP栈和 Linux网络栈可互换使用,因为 TCP/IP栈是 L ...
- MyEclipse的Debug模式启动缓慢
打开breakpoints veiw,右键-> Remove all,重启下服务器就OK了 -–下面有个"顶"字,你懂得O(∩_∩)O哈哈~ -–乐于分享,共同进步! -–更 ...
- Matlab to OpenCV Mat
convert Matlab matrix to OpenCV Mat. Support CV_32FC3 only currently. The Code int matlab2opencv(cv: ...
- JAVA面向对象-----final关键字
JAVA面向对象-–final关键字 1:定义静态方法求圆的面积 2:定义静态方法求圆的周长 3:发现方法中有重复的代码,就是PI,圆周率. 1:如果需要提高计算精度,就需要修改每个方法中圆周率. 4 ...
- 抽屉效果的实现(DrawerLayout和SlidingMenu的对比)
在做谷歌电子市场的时候用的是DrawerLayout实现的抽屉效果,在新闻客户端的时候用的是开源框架SlidingMenu来实现的,总的来说,各有个的优点,侧滑(开源框架)实现的效果更好,但是Draw ...
- Android布局性能优化—从源码角度看ViewStub延迟加载技术
在项目中,难免会遇到这种需求,在程序运行时需要动态根据条件来决定显示哪个View或某个布局,最通常的想法就是把需要动态显示的View都先写在布局中,然后把它们的可见性设为View.GONE,最后在代码 ...
- android开发之AlertDialog点击按钮之后不消失
最近有这样一个需求,我需要用户在一个弹出框里输入密码来验证,验证成功当然好说,但是如果验证失败则需要把alertdialog的标题改为"密码错误,请重新输入",并且这个alertd ...
- 04 Spinner 列表选中
<span style="font-size:18px;"> <?xml version="1.0" encoding="utf-8 ...