题目描述:

'?' 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. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(8)-MVC与EasyUI DataGrid 分页

    系列目录 前言 为了符合后面更新后的重构系统,文章于2016-11-1日重写 EasyUI Datagrid在加载的时候会提交一些分页的信息到后台,我们需要根据这些信息来进行数据分页再次返回到前台 实 ...

  2. ASP.NET MVC5----了解我们使用的@HTML帮助类

    20几岁,怕什么. 自己的感觉 说一个自己最近使用AngularJS的感受,我们之前使用mvc进行项目开发都是了解和经常使用HTML的帮助类,来完成我们前端大部分代码的编写,其实在我没有接触Angul ...

  3. 移动端API接口优化的术和结果

    最近一直在忙工作的事情,所以文章写得有些少. 有3-5篇文章都是写到一半然后被别的事情给打断了,所以,我得找个时间好好补补. 最近一直在关注移动端接口API的可用性问题,在移动时代这个做这个优化能产生 ...

  4. source /etc/profile报错-bash: id:command is not found

    由于误操作导致 source /etc/profile 报错 -bash: id:command is not found 此时,linux下很多命令到不能能用,包括vi ls 等... 可以使用 e ...

  5. tomcat 504 gateway time-out

    今天有个环境ajax调用一个请求的时候,出现一个504 gateway time-out响应,原以为是nginx找不到资源的问题,恰当我们的服务器上又配置了nginx,看了配置文件,没有指向tomca ...

  6. 遍历datatable的方法汇总

    遍历datatable的方法方法一: DataTable dt = dataSet.Tables[]; ; i < dt.Rows.Count ; i++) { string strName = ...

  7. H5 WebSocket 如何和C#进行通信

    HTML5作为下一代的 Web 标准, 拥有许多引人注目的新特性,如 Canvas.本地存储.多媒体编程接口.WebSocket 等.WebSocket 在浏览器和服务器之间提供了一个基于 TCP 连 ...

  8. 浅谈Angular的 $q, defer, promise

    浅谈Angular的 $q, defer, promise 时间 2016-01-13 00:28:00  博客园-原创精华区 原文  http://www.cnblogs.com/big-snow/ ...

  9. 谈谈我对前端组件化中“组件”的理解,顺带写个Vue与React的demo

    前言 前端已经过了单兵作战的时代了,现在一个稍微复杂一点的项目都需要几个人协同开发,一个战略级别的APP的话分工会更细,比如携程: 携程app = 机票频道 + 酒店频道 + 旅游频道 + ..... ...

  10. 3.2 js六大数据类型

    js中有六种数据类型,包括五种基本数据类型(Number,String,Boolean,Null,Undefined),和一种混合数据类型(Object). 前面说到js中变量是松散类型的,因此有时候 ...