题目描述:

'?' 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 MVC5+EF6+EasyUI 后台管理系统(63)-Excel导入和导出

    系列目录 昨天文章太过仓促没有补充导出的示例源码,在者当时弄到到很晚没时间做出导出功能,对阅读理解造成影响,现补充一份示例源码,顺便补充导出的功能说明,望理解 示例代码下载   https://yun ...

  2. 当master down掉后,pt-heartbeat不断重试会导致内存缓慢增长

    最近同事反映,在使用pt-heartbeat监控主从复制延迟的过程中,如果master down掉了,则pt-heartbeat则会连接失败,但会不断重试. 重试本无可厚非,毕竟从使用者的角度来说,希 ...

  3. egret3D与2D混合开发,画布尺寸不一致的问题

    egret3d的GUI目前还没有,在做3d游戏的时候没有UI可用,只能使用egret2d的EUI组件库,egret3d与egret2d混合开发,canvas3d的大小与位置与canvas2d并没有重合 ...

  4. Javascript 中 with 的替代方案和String 中的正则方法

    这几天在升级自己的MVVM 框架,遇到很多小问题,就在这里统一解决了. with 语法 在代码中,要执行这么一个函数 function computeExpression(exp, scope) { ...

  5. Java 快速排序两种实现

    快速排序,只要学习过编程的人肯定都听说过这个名词,但是有时候写的时候还真蒙住了,网上搜罗了下以及查阅了"introduction to algorithm",暂时找到两种实现快排的 ...

  6. javaWeb https连接器

    互联网加密原理 tomcat服务器启动时候会启动多个Connector(连接器),而Tomcat服务器的连接器又分为加密连接器和非加密连接器 .(一般我们使用http协议的是非加密,https的是加密 ...

  7. 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件

    [源码下载] 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件 作者 ...

  8. J2EE项目开发中常用到的公共方法

    在项目IDCM中涉及到多种工单,包括有:服务器|网络设备上下架工单.服务器|网络设备重启工单.服务器光纤网线更换工单.网络设备撤线布线工单.服务器|网络设备替换工单.服务器|网络设备RMA工单.通用原 ...

  9. PowerDesigner从Sqlserver中反转为带注释的字典及快捷键操作

    PowerDesigner的操作经常忘记,所以把常用的功能记录下来备忘. 1.修改反转过来的字段 PowerDesigner从数据库反转的时候,默认不带注释,需要先进行修改. 输入如下脚本: {OWN ...

  10. 做linux运维工程师,必须要掌握以下几个工具

    linux系统如果是学习可以选用redhat或centos,特别是centos在企业中用得最多,当然还会有其它版本的,但学习者还是以这2个版本学习就行,因为这两个版本都是兄弟,没区别的,有空可以再研究 ...