【一天一道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 ...
随机推荐
- Node.js ZLIB
Zlib 稳定性: 3 - 文档 可以通过以下方式访问这个模块: var zlib = require('zlib'); 这个模块提供了对 Gzip/Gunzip, Deflate/Inflate, ...
- PHP 实例 AJAX 与 MySQL
AJAX 数据库实例 下面的实例将演示网页如何通过 AJAX 从数据库读取信息: 实例 Person info will be listed here... 实例解释 - MySQL 数据库 在上 ...
- C实战:项目构建Make,Automake,CMake
C实战:项目构建Make,Automake,CMake 在本系列文章<C实战:强大的程序调试工具GDB>中我们简要学习了流行的调试工具GDB的使用方法.本文继续"C实战" ...
- Matlab—regexp正则表达式
原文转自:http://blog.csdn.net/yf210yf/article/details/42421523 关于正则表达式的基本知识 正则表达式就是一个表达式(也是一串字符),它定义了某种字 ...
- RX系列四 | RxAndroid | 加载图片 | 提交表单
RX系列四 | RxAndroid | 加载图片 | 提交表单 说实话,学RxJava就是为了我们在Android中运用的更加顺手一点,也就是RxAndroid,我们还是先一步步来,学会怎么去用的比较 ...
- Android开发技巧——定制仿微信图片裁剪控件
拍照--裁剪,或者是选择图片--裁剪,是我们设置头像或上传图片时经常需要的一组操作.上篇讲了Camera的使用,这篇讲一下我对图片裁剪的实现. 背景 下面的需求都来自产品. 裁剪图片要像微信那样,拖动 ...
- Objective-C特有类型——id
Objective-C特有类型--id OC里,id和int.double等一样,是一个类型 不同的是: id是一个万能指针,能指向/操作任何OC对象 相当于 (NSObject *) 用法 id i ...
- 剑指Offer——“你最大的缺点是什么”回答技巧及范例
剑指Offer--"你最大的缺点是什么"回答技巧及范例 问题分析:认识自己的缺点是一个巨大的优点, 当HR问到你缺点的时候, 你的机会来了, 请快展示你的自知之明吧!你想把优点 ...
- 微信小程序基本组件概述
为了更好的理解微信小程序,本文90%文字描述来源于官网的介绍.官网原链接https://mp.weixin.qq.com/debug/wxadoc/dev/component/?t=20161222 ...
- Python 通过继承实现标准对象的子类
idict是dict的子类,它的键值和属性是同步的,并且有强大的默认值机制. 例如,假设x是idict的一个实例,且x['a']['b']=12,则有x.a.b=12.反之亦然; 假设'c'不在x的键 ...