题目描述:

'?' 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

分析:这道题有点类似与简单的正则表达式引擎。我们用两个指针分别标记s和p,若当前位置的元素匹配,则两个指针都向右移动一个数。如果遇到'*',则p的指针指向后面不为'*'的第一个字符,再对s和p当前的指针位置做一个标记,标记为preS,preP,我们可以认为在当前指针之前的s和p是匹配的(感觉像动态规划的思想)。如果不匹配,则分两种情况进行讨论:1、前面有'*',则先将PreS++,再将两个指针分别移回preS和preP,其实就是匹配失败,移向下一位再比较一次 2、前面没有'*',则匹配失败,返回false。最后,当s的指针指向最末尾,则比较结束。如果p的指针之后还有非'*'的字符,则匹配失败,否则匹配成功。

代码:

bool isMatch(char* s, char* p) {
int indexS=0,indexP=0;
int preS,preP;
bool star=false; while(indexS<strlen(s)) {
if(p[indexP]=='?') {
indexS++,indexP++;
} else if(p[indexP]==s[indexS]){
indexS++,indexP++;
} else if(p[indexP]=='*') {
star=true;
while(p[indexP]=='*') {
indexP++;
}
if(indexP==strlen(p)) {
return true;
}
preS=indexS;
preP=indexP;
} else {
if(star==false) {
return false;
}
preS++;
indexS=preS;
indexP=preP;
}
}
while(p[indexP]=='*') {
indexP++;
}
if(indexP==strlen(p)) {
return true;
} else {
return false;
} }

  

LeetCode题解-----Wildcard Matching的更多相关文章

  1. 【leetcode】Wildcard Matching

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

  2. LeetCode - 44. Wildcard Matching

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

  3. LeetCode 044 Wildcard Matching

    题目要求:Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches ...

  4. Java for LeetCode 044 Wildcard Matching

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

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

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

  6. 【leetcode】Wildcard Matching(hard) ★ 大神太牛了

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

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

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

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

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

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

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

随机推荐

  1. JavaScript语言精粹--执行环境及作用域,this

    1.执行环境定义了变量或函数有权访问的其他数据,决定了他们各自的行为. 每个执行环境都有一个与之关联的变量对象,环境中定义的所有变量和函数都保存在这个对象中. 虽然我们无法访问,但是解析器在处理数据时 ...

  2. 引用MVC源码的小问题

    vs2010自己创建的MVC项目,排除掉System.Web.Mvc引用后,引用源码,直接运行会提示引用冲突,这个是因为配置文件的问题,只要把配置文件中assemblies节点下的 <add a ...

  3. MVC5 网站开发之五 展示层架构

    展示层由Ninesky.Web项目实现,负责网站内容的显示,项目包含Member和Control两个区域.   目录 奔跑吧,代码小哥! MVC5网站开发之一 总体概述 MVC5 网站开发之二 创建项 ...

  4. 你可能不知道的陷阱, IEnumerable接口

    1.  IEnumerable 与  IEnumerator IEnumerable枚举器接口的重要性,说一万句话都不过分.几乎所有集合都实现了这个接口,Linq的核心也依赖于这个万能的接口.C语言的 ...

  5. python2.7 内置ConfigParser支持Unicode读写

    1 python编码基础 对应 C/C++ 的 char 和 wchar_t, Python 也有两种字符串类型,str 与 unicode: str与unicode # -*- coding: ut ...

  6. 【分布式】Zookeeper使用--Java API

    一.前言 上一篇博客我们通过命令行来操作Zookeper的客户端和服务端并进行相应的操作,这篇主要介绍如何通过API(JAVA)来操作Zookeeper. 二.开发环境配置 首先打开Zookeeper ...

  7. JWT实现token-based会话管理

    上文<3种web会话管理的方式>介绍了3种会话管理的方式,其中token-based的方式有必要从实现层面了解一下.本文主要介绍这方面的内容.上文提到token-based的实现目前有一个 ...

  8. ASP.NET MVC 发送邮件(异步)

    最近写邮件发送搞死人了,最后的结果,真是醉了,现整理如下: 网上一搜一大把,到处都是.NET发送邮件的方法,我这里也大同小异的写了一个. 准备一个MailHelper.cs通用类,如下所示: 重要的命 ...

  9. SQL SERVER 多数据导入

    public bool CreateTB_PROPERTY_MODELLByList(List<TB_PROPERTY_MODEL> entity) { try { //将集合转换成Dat ...

  10. 【C#】无损转换Image为Icon

    如题,市面上常见的方法是: var handle = bmp.GetHicon(); //得到图标句柄 return Icon.FromHandle(handle); //通过句柄得到图标 此法的问题 ...