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

这题和正则表达式匹配有点类似,' . '和本题中的' ? '一样,区别在于' * ',在正则表达式那题中,' * '可以表示之前字符的0个或多个;本题中,可以表示任意字符串。好吧,做了,第一次做的时候,没有完全搞懂,这次出问题了,又去网上找帮助了。本文章参考了peerlessbloom 、点缀星辰Grandyang

思路:当*s ==*p || *p=='? '时,对应的指针分别++即可;当遇到字符' * '时,该怎么办呢?是否像正则表达式那题中的思路把 *s和*(p+1)去比较了?若是这样的,说明没有理解清楚:题目中' * '可以表示任意字符串的含义,举个例子来说明s=" aacabacd "和 p=" a*acd ",这种情况下,若是按上题中的思想,则要返回false,但是本题这种情况确是匹配的。这题中,' * '和' acab '匹配了。这如何实现的了?我们先看*号匹配的几种情况(例子来自 点缀星辰的博客,这里只是结合自己的语言给出了):

1)*号匹配了串 s 中间指定点秩后所有的字符。这里*号匹配了“bcac”。

2)*号匹配部分串的情况。这里*号匹配了“bc”。这种情况就是字符串p中的*号之后的字符' a '和*号对应的S中的字符' b '去匹配,若不匹配,则比较s 中的下一个,直到匹配。

那是否所有的情况就结束了?如果是这样的,就和正则表达式的那种情况,很相似了,好吧,我当时就是这么想的,错了。还有其他的情况,如:

3)如下图1,刚开始,*号的下一个和*号对应的S中的字符' a '匹配,这意味着*号代表0个字符,直到' d '和 ' b '不匹配,那这种情况,怎么办了?是否返回false?这种情况显然不会返回false,因为,图中绿色的部分是匹配的,这也说明了一个问题,就是*号代表0个字符这种形式是不对了。那该如何?接下来应该如图2所示,p中字符a跟s中字符c 对比,不匹配,再跟s中字符b对比,直到在s中遇到字符a,即匹配,这样,p和s同时的比较下一个字符。若后面还有*号,能遍历到*号,说明*号之前都是匹配的,这样只要重复上述过程即可。

图1

图2

这就遇到一个问题了?当匹配了若干字符之后,才发现不匹配,如情况3),这时如何像上述过程那样,比较p中*号的下一位和s中*号对应的那位的下一位了?这时,我们采取的方法是,当遇到*号时,我们将使用两指针记录下此时的值,若在以后的对比中,遇到不匹配的情况,即可返回。当然,p中*号的位置是相对不变的(直到遇到下一*号),而s中用来的对比字符是在变化的。代码如下:

 class Solution {
public:
bool isMatch(const char *s, const char *p)
{
const char *preS=NULL; //用来记录位置
const char *preP=NULL; while(*s)
{
if(*p=='?' || *p==*s)
{
p++;
s++;
}
else if(*p=='*')
{
preS=s;
preP=p++;
}
else if(preP) //后面遇到不匹配的情况,则返回重新比较情况3)
{
p=preP+;
s=++preS;
}
else
return false;
}
while(*p=='*')
p++;
return !*p;
}
};

[Leetcode] Wildcard matching 通配符匹配的更多相关文章

  1. [LeetCode]Wildcard Matching 通配符匹配(贪心)

    一開始採用递归写.TLE. class Solution { public: bool flag; int n,m; void dfs(int id0,const char *s,int id1,co ...

  2. [LeetCode] Wildcard Matching 外卡匹配

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  3. 044 Wildcard Matching 通配符匹配

    实现一个支持 '?' 和 '*' 的通配符匹配.'?' 匹配任何单个字符.'*' 匹配任何数量的字符 (包括0个).匹配应覆盖 整个 输入字符串(而不是部分).这个函数原型为:bool isMatch ...

  4. [LeetCode] 44. Wildcard Matching 外卡匹配

    Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...

  5. LeetCode: Wildcard Matching 解题报告

    Wildcard MatchingImplement wildcard pattern matching with support for '?' and '*'. '?' Matches any s ...

  6. [LeetCode] Wildcard Matching 题解

    6. Wildcard Matching 题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matche ...

  7. [Leetcode] Wildcard Matching

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  8. [LeetCode] Wildcard Matching 字符串匹配,kmp,回溯,dp

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  9. [leetcode]Wildcard Matching @ Python

    原题地址:https://oj.leetcode.com/problems/wildcard-matching/ 题意: Implement wildcard pattern matching wit ...

随机推荐

  1. php-语言参考-类型3.2-未完待续

    一,PHP变量的8个类型 四种标量类型: boolean (布尔型) integer (整型) float (浮点型, 也称作 double) string (字符串) 两种复合类型: array ( ...

  2. 初学tiny4412

    1.解压友善之臂提供的uboot make tiny4412_config make 然后将sd卡插到电脑上,编辑虚拟机,选择对应的usb口(usb3.0兼容),如果没有usb3.0,可能是虚拟机版本 ...

  3. pyc是个什么鬼?

    1.Python是一门解释型语音? 我初学Python时,听到的关于Python的第一句话就是,Python是一门解释型语音,我就这样一直相信下去,知道发现了*.pyc文件的存在.如果是解释型语音,那 ...

  4. queue消息队列

    class queue.Queue(maxsize=0) #先入先出 class queue.LifoQueue(maxsize=0) #last in fisrt out  class queue. ...

  5. python--基本类型之数值

    Number(数字): 数字类型创建: a = 10b = ab = 20 pint('a : 'a)pint('b : 'b) 数据类型转换: int(x,[,base]) 将 x 转换为一个整数f ...

  6. React16版本的新特性

    React16版本更新的新特性 2018年05月03日 21:27:56 阅读数:188 1.render方法的返回值类型:New render return types 之前的方式: class A ...

  7. Vue-router使用

    Vue路由:--------------------------------------------------------1 .Vue-rouer入门2 .子路由3 .路由传参4 .多路由区域操作5 ...

  8. nginx proxy_cache缓存详解

    目录 1. 关于缓冲区指令 1.1 proxy_buffer_size 1.2 proxy_buffering 1.3 proxy_buffers 1.4 proxy_busy_buffers_siz ...

  9. 玩转VIM-札记(三)

    玩转VIM-札记(三) 眨眼之间,5月就要从指间溜走,不给人一点点遐想的时间,我要赶紧抓着五月的尾巴,在博客中在添一笔.那么就还接着Vim来说吧.以Vim来为五月画上一个句号. 返璞归真 相信经过玩转 ...

  10. python 网络篇(计算机网络基础)

                               计算机网络的发展及基础网络概念                    广播 主机之间“一对所有”的通讯模式,网络对其中每一台主机发出的信号都进行无 ...