一天一道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. PHP 完整表单实例

    PHP - 在表单中确保输入值 在用户点击提交按钮后,为确保字段值是否输入正确,我们在HTML的input元素中插添加PHP脚本, 各字段名为: name, email, 和 website. 在评论 ...

  2. 初识Vulkan

    Vulkan是Khronos组织制定的"下一代"开放的图形显示API,是与DirectX12可以匹敌的GPU API标准.Vulkan是基于AMD的Mantle API演化而来,目 ...

  3. 【mybatis深度历险系列】深入浅出mybatis中原始dao的开发和mapper代理开发

    使用Mybatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper接口开发方法.mybatis在进行dao开发的时候,涉及到三姐妹,分别是SqlSessionFactoryBuilder ...

  4. 如何对n个大小都小于100的整数进行排序,要求时间复杂度O(n),空间复杂度O(1)。

    提示:hash表 #include <iostream> using namespace std; #define N 100 #define RANGE 100 int* getRand ...

  5. 前端面试题-----js和jquery的区别是什么?

    最近我有一个朋友问我js和jquery的区别是什么,于是我打算写一篇文章说下到底有什么区别. 首先你要知道: 1.js是网页的脚本语言,记住哈,js是语言! 2.jquery是用js语言写出来的一个框 ...

  6. C控制台实现模拟平抛运动算法

    平抛运动这个相信读了高中物理都知道这个概念了,详细的我就不说了,不明白的看看百度: 平抛运动 接下来看看用控制台实现的平抛运动算法: #include <stdio.h> #include ...

  7. mapdb的一些性能测试

    jdk1.6,8g,64位,Intel Core i5-4210U CPU @ 1.70GHz 2.40GHz 使用memorydb 100个htreemap,每个htreemap对应50条线程操作, ...

  8. Tomcat集群应用部署的实现机制

    集群应用部署是一个很重要的应用场景,设想一下如果没有集群应用部署功能,每当我们发布应用时都要登陆每台机器对每个tomcat实例进行部署,这些工作量都是繁杂且重复的,而对于进步青年的程序员来说是不能容忍 ...

  9. 06 Activity 4中启动模式

    前言:改变Activity的启动模式可以清单文件AndroidManifest的Activity标签添加属性android:launchMode="standard"中修改如下图: ...

  10. Java四大名著下载大全(中文+英文)

    转自:http://www.blogjava.net/kuuyee/archive/2013/06/03/400084.html 抽时间整理了一下Java四大名著,分享出来方便大家学习! Note 郑 ...