题目描述:

'?' 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. 【分布式】Zookeeper系统模型

    一.前言 前面已经讲解了Zookeeper的一些应用场景,但是并没有深入到Zookeeper内部进行分析,本篇将讲解其系统模型. 二.系统模型 2.1 数据模型 Zookeeper的数据节点称为ZNo ...

  2. [收藏]IntelliJ Idea快捷键

    Alt+回车 导入包,自动修正 Ctrl+N 查找类 Ctrl+Shift+N 查找文件 Ctrl+Alt+L 格式化代码 Ctrl+Alt+O 优化导入的类和包 Alt+Insert 生成代码(如g ...

  3. 编写高质量代码:改善Java程序的151个建议(第4章:字符串___建议56~59)

    建议56:自由选择字符串拼接方法 对一个字符串拼接有三种方法:加号.concat方法及StringBuilder(或StringBuffer ,由于StringBuffer的方法与StringBuil ...

  4. 工作中遇到的一个多线程下导致RCW无法释放的问题

    最近有个同事在调用一个类库中的方法时遇到了一个问题,异常信息如下: 尝试释放正在使用的RCW,活动线程或其他线程上正在使用该 RCW,释放正在使用的 RCW 的尝试会导致损坏或数据丢失. 该方法中对w ...

  5. iOS 编辑UITableView(根据iOS编程编写)

    上个项目我们完成了 JXHomepwner 简单的应用展示,项目地址.本节我们需要在上节项目基础上,增加一些响应用户操作.包括添加,删除和移动表格. 编辑模式 UITableView 有一个名为  e ...

  6. asp.net core 依赖注入问题

    最近.net core可以跨平台了,这是一个伟大的事情,为了可以赶上两年以后的跨平台部署大潮,我也加入到了学习之列.今天研究的是依赖注入,但是我发现一个问题,困扰我很久,现在我贴出来,希望可以有人帮忙 ...

  7. 再谈JavaScript闭包及应用

    .title-bar { width: 80%; height: 35px; padding-left: 35px; color: white; line-height: 35px; font-siz ...

  8. PDF编辑神器

    转自网络 http://files.cnblogs.com/files/quejuwen/pdfeditportable.zip

  9. 【神器】vimum在浏览器中键盘操作选择、复制、粘贴,键盘党的最爱

    1.下载: http://files.cnblogs.com/files/quejuwen/vimum_extension_1_56.zip 2.开源:https://github.com/philc ...

  10. VS资源编辑器常见错误RC1000到RC1208

    资源编译器错误RC1000到RC1208 以下主题包括资源编译器错误RC1000至RC1208: 资源编译器致命错误RC1000未知致命错误记下错误的情况下,尝试找出问题,并创建一个重现的测试案例,然 ...