一天一道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的更多相关文章

  1. LeetCode - 44. Wildcard Matching

    44. Wildcard Matching Problem's Link --------------------------------------------------------------- ...

  2. 第八周 Leetcode 44. Wildcard Matching 水题 (HARD)

    Leetcode 44 实现一种类似正则表达式的字符串匹配功能. 复杂度要求不高, 调代码稍微费点劲.. 好像跟贪心也不太沾边, 总之 *把待匹配串分成若干个子串, 每一个子串尽量在模式串中靠前的部分 ...

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

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

  4. [leetcode]44. Wildcard Matching万能符匹配

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

  5. LeetCode 44 Wildcard Matching(字符串匹配问题)

    题目链接:https://leetcode.com/problems/wildcard-matching/?tab=Description   '?' Matches any single chara ...

  6. leetcode 44. Wildcard Matching(模糊匹配)

    搬运工了- - https://blog.csdn.net/jmspan/article/details/51460021

  7. leetcode 10. Regular Expression Matching 、44. Wildcard Matching

    10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...

  8. 44. Wildcard Matching

    题目: Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single charact ...

  9. 【LeetCode】44. Wildcard Matching (2 solutions)

    Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...

随机推荐

  1. windows资源管理器中配置右键bash here

    windows下安装了git后有git bash here 但是安装了cygwin没有bash here 我们可以通过修改注册表的方式自己做一个 Win10下在注册表内有一般有两个默认的 cmd 和 ...

  2. 第一篇博客 ---- 分享关于Maven使用的一些技巧

    Maven环境搭建 在官网上下载maven安装包,地址:http://maven.apache.org/download.cgi . 解压文件到电脑坐在盘符目录,如E:\apache-maven-3. ...

  3. Node.js 教程

    简单的说 Node.js 就是运行在服务端的 JavaScript. Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台. Node.js是一个事件驱动I/O服务端Ja ...

  4. CMS垃圾收集器

    介绍 CMS垃圾回收器的全称是Concurrent Mark-Sweep Collector,从名字上可以看出两点,一个是使用的是并发收集,第二个是使用的收集算法是Mark-Sweep.从而也可以推测 ...

  5. TensorFlow实验环境搭建

    初衷: 由于系统.平台的原因,网上有各种版本的tensorflow安装教程,基于linux的.mac的.windows的,各有不同,tensorflow的官网也给出了具体的安装命令.但实际上,即使te ...

  6. XCode使用技巧

    XCode使用技巧 自动生成get.set方法 @property 用法 #import <Foundation/Foundation.h> @interface People : NSO ...

  7. Android 5.0 调色 Palette调色功能

    Palette非常好用,也非常好玩. Palette的作用是从图像中提取突出的颜色,这样我们可以根据提取到的色值把它赋给Toolbar,标题,状态栏等,可以使我们的整个界面色调统一,效果非常好看. P ...

  8. FORM内置系统函数

    abort_query;                                                    停止查询的执行 add_group_column(record grou ...

  9. Scikit-learn:模型选择Model selection

    http://blog.csdn.net/pipisorry/article/details/52250983 选择合适的estimator 通常机器学习最难的一部分是选择合适的estimator,不 ...

  10. 关于ROS学习的一些反思

    距离发布上一篇ROS的博客已经过去两年了,才发现原来自己已经这么久可没有写过关于ROS的文章,想来很是惭愧.这两年时间,自己怀着程序员的梦想,研究过RTOS,探索过Linux,编写过Android应用 ...