一天一道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. Node.js 调试器

    稳定性: 3 - 稳定 V8 提供了强大的调试工具,可以通过 TCP protocol 从外部访问.Node 内置这个调试工具客户端.要使用这个调试器,以debug参数启动 Node,出现提示: % ...

  2. Python3 OS 文件/目录方法

    os 模块提供了非常丰富的方法用来处理文件和目录.常用的方法如下表所示: 序号 方法及描述 1 os.access(path, mode) 检验权限模式 2 os.chdir(path) 改变当前工作 ...

  3. linux网络编程之二-----多播(组播)编程

    多播编程实例 服务器端 下面是一个多播服务器的例子.多播服务器的程序设计很简单,建立一个数据包套接字,选定多播的IP地址和端口,直接向此多播地址发送数据就可以了.多播服务器的程序设计,不需要服务器加入 ...

  4. log file sync 等侍值高的一般通用解决办法

    log file sync等待时间发生在redo log从log buffer写入到log file期间. 下面对log file sync做个详细的解释. 何时发生日志写入: 1.commit或者r ...

  5. Compass 更智能的搜索引擎(3)--高亮,排序,过滤以及各种搜索

    要想使得一个搜索系统更加的完美,查询精确度和页面显示算是其中比较重要的两个方面.今天,我们就来谈谈怎么使得我们的搜索系统更加的完美. 关于分词 下载地址 配置 关于高亮 关于排序 原理 冗余字段 使用 ...

  6. ROS(indigo)RRT路径规划

    源码地址:https://github.com/nalin1096/path_planning 路径规划 使用ROS实现了基于RRT路径规划算法. 发行版 - indigo 算法在有一个障碍的环境找到 ...

  7. JAVA面向对象-----instanceof 关键字

    instanceof 关键字 1:快速演示instanceof Person p=new Person(); System.out.println( p instanceof Person); 2:i ...

  8. shape图形的使用

    shape图形的使用 在项目中如果用到有规律的常规的图形,在能够掌握的前提下建议使用shape图形,shape图形相对与图片来说,占用资源更小,并且使用起来不会失真. 效果图 shape图形1 < ...

  9. TCP的发送系列 — 发送缓存的管理(二)

    主要内容:从TCP层面判断发送缓存的申请是否合法,进程因缺少发送缓存而进行睡眠等待. 因为有发送缓存可写事件而被唤醒. 内核版本:3.15.2 我的博客:http://blog.csdn.net/zh ...

  10. 【编程练习】最近准备开始找工作,这篇文章作为一个code练手题目的总结吧

    找工作时候一般需要准备的算法题目类型,其实参考leetcode和poj或者剑指offer基本能够摆平大部分的题目了 1.图的遍历,BFS.DFS: 2.递归的回溯剪枝: 3.树的建立和遍历: 4.状态 ...