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

我的思路:不提了,太挫了,写了100多行代码都没搞定,直接看大神10行搞定的代码吧:

其实主要的问题就在于p中的*究竟代表哪几个字母,大神的代码中用ss记录*代表字母的后面一个位置,star记录p中*的位置。

先假设*代表0个字符,如果后面发现不成立,再返回来

设*代表1个字符,............................

bool isMatch(const char *s, const char *p) {
const char* star=NULL;
const char* ss=s;
while (*s){
//advancing both pointers when (both characters match) or ('?' found in pattern)
//note that *p will not advance beyond its length
if ((*p=='?')||(*p==*s)){s++;p++;continue;} // * found in pattern, track index of *, only advancing pattern pointer
if (*p=='*'){star=p++; ss=s;continue;} //current characters didn't match, last pattern pointer was *, current pattern pointer is not *
//only advancing pattern pointer
if (star){ p = star+; s=++ss;continue;} //current pattern pointer is not star, last patter pointer was not *
//characters do not match
return false;
} //check for remaining characters in pattern
while (*p=='*'){p++;} return !*p;
}

【leetcode】Wildcard Matching(hard) ★ 大神太牛了的更多相关文章

  1. LeetCode: Wildcard Matching 解题报告

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

  2. [LeetCode] Wildcard Matching 题解

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

  3. [LeetCode] Wildcard Matching 外卡匹配

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

  4. [Leetcode] Wildcard Matching

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

  5. [leetcode]Wildcard Matching @ Python

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

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

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

  7. [Leetcode] Wildcard matching 通配符匹配

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

  8. leetcode Wildcard Matching greedy algrithm

    The recursive program will result in TLE like this: class Solution { public: bool isMatch(const char ...

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

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

随机推荐

  1. Junit初级编码(二)探索JUnit核心

    序,Junit测试是单元测试的一个框架,提供了很多方法,供我们快速开展单元测试.现在就让我们慢慢学习Junit单元测试框架 一.Junit的三个核心概念测试类.测试集.测试运行器 1 测试类 公共的, ...

  2. NUGet的诞生与使用

    本文引用地址:http://msdn.microsoft.com/zh-cn/magazine/hh547106.aspx NuGet 使用 NuGet 管理项目库 Phil Haack 无论多么努力 ...

  3. POJ 3292 Semi-prime H-numbers

    类似素数筛... Semi-prime H-numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6873 Accept ...

  4. Ubuntu 14 Chrome字体设置备份

    Ubuntu 14 Chrome字体设置备份 1.设置 -> 显示高级设置 -> 自定义字体 -> (1) 标准字体:YaHei Consolas Hybrid,14 (2) Ser ...

  5. 采用Atlas+Keepalived实现MySQL读写分离、读负载均衡【转载】

      文章 原始出处 :http://sofar.blog.51cto.com/353572/1601552 ============================================== ...

  6. springmvc之DispatcherServlet

    1.作用:DispatcherServlet是前置控制器,配置在web.xml(因为DispatcherServlet是一个servelet)文件中的.拦截匹配的请求,Servlet拦截匹配规则要自已 ...

  7. Redis学习笔记七:独立功能之排序

    sort 命令可以对列表键.集合键或有序集合键的值进行排序.sort 命令并不修改数据库值,只是输出有序. 127.0.0.1:6379> rpush numbers 9 8 7 6 1 2 3 ...

  8. 联不上网 Unable to initialize Windows Sockets interface. General failure.

    电脑莫名联不上网 Unable to initialize Windows Sockets interface. General failure. Unable to initialize the W ...

  9. mongodb的sql例子(简单版)

    插入数据 db.person.insert({"name":"zfx","age":21}) 查找所有数据 db.person.find() ...

  10. 制作自定义背景Button按钮、自定义形状Button的全攻略(转)

    在Android开发应用中,默认的Button是由系统渲染和管理大小的.而我们看到的成功的移动应用,都是有着酷炫的外观和使用体验的.因此,我们在开发产品的时候,需要对默认按钮进行美化.在本篇里,笔者结 ...