Wildcard Matching

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
 
类似于Regular Expression Matching,会超时
 
 class Solution {
public:
bool isMatch(const char *s, const char *p) { if(*s=='\0'&&*p=='\0') return true;
if(*s!='\0'&&*p=='\0') return false;
if(*s=='\0'&&*p!='\0') return false; if(*p=='*')
{
while(*p=='*') p++; while(*s!='\0')
{
if(isMatch(s,p)) return true;
s++;
} return isMatch(s,p);
}
else if(*p=='?')
{
return isMatch(s+,p+);
}
else
{
return (*s==*p&&isMatch(s+,p+));
} return false;
}
};
 
 
 
采用类似回溯的思想
 
依次对字符进行比对,如果发现p=='*'
则从(p+1)开始和s开始依次比较,如果发现不对,
则回溯到(p+1)的位置,再从s+1开始依次比较
如果再不对,则再次回溯到(p+1)的位置,从s+2位置开始依次比较
……
 

 class Solution {
public:
bool isMatch(const char *s, const char *p) { const char* afterStarPositionP=NULL;
const char* afterStarPositionS=NULL;
while(*s!='\0')
{
if(*s==*p||*p=='?')
{
s++;p++;
continue;
}
else if(*p=='*')
{
p++;
afterStarPositionP=p;
afterStarPositionS=s;
}
else
{
if(afterStarPositionP!=NULL)
{
afterStarPositionS++;
s=afterStarPositionS;
p=afterStarPositionP;
}
else
{
return false;
}
}
} while(*p=='*')
{
p++;
} if(*p=='\0') return true;
else return false;
}
};
 
 

【leetcode】Wildcard Matching的更多相关文章

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

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

  2. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  3. 【LeetCode】回溯法 backtracking(共39题)

    [10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses ( ...

  4. 【LeetCode】贪心 greedy(共38题)

    [44]Wildcard Matching [45]Jump Game II (2018年11月28日,算法群衍生题) 题目背景和 55 一样的,问我能到达最后一个index的话,最少走几步. 题解: ...

  5. 【leetcode】44. Wildcard Matching

    题目如下: 解题思路:本题和[leetcode]97. Interleaving String非常相似,同样可以采用动态规划的方法.记dp[i][j] = 1或者0 表示pattern[0:i]是否匹 ...

  6. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  7. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  8. 【Leetcode】Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  9. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

随机推荐

  1. 防SQL注入代码(ASP版)

    <% Dim Fy_Url,Fy_a,Fy_x,Fy_Cs(),Fy_Cl,Fy_Ts,Fy_Zx '---定义部份 头------ Fy_Cl = 1 '处理方式:1=提示信息,2=转向页面, ...

  2. AndroidManiFast 字段意义

    每个Activity都要在本文件中注册. <Activity>下的<Intent-filter>中. 两个字段的意思是: <action android:name=&qu ...

  3. STM32向量表详细分析

    预备知识: DCD指令:用于分配一片连续的字存储单元(32bit),并将表达式的值初始化给该字存储单元,类似于C中定义数组并初始化.比如: DCD 0 的意思是:分配一个字存储单元,并将该单元初始化为 ...

  4. Nginx反向代理模板

    nginx反向代理模板,修改nginx.conf #Basic reverse proxy server # upstream wcf{    server localhost:8080; #wcf1 ...

  5. 工作中linux定时任务的设置及相关配置

    工作中会用到定时任务,来处理以前采集来的数据备份, 每周一凌晨4点执行一次    0 4 * * */1 find/data/templatecdr/oracle/dcndatabak/ -type ...

  6. Myeclipse如何关联源码

    Myeclipse版本:Myeclipse2014 关联源码前要下载对应的源码,如本例的dom4j-1.6.1.jar,则去下载对应的源码dom4j-1.6.1.zip 如果做的是web项目,就要将该 ...

  7. dwz 在dialog里打开dialog

    需要在打开dialog里再弹出一个dialog的话,需要在打开第一个dialog的地方指定rel,这样就可以弹出第二个dialog而不是替换掉第一个dialog <a class="a ...

  8. Spark之Streaming

    1. socket消息发送 import java.net.ServerSocket import java.io.PrintWriter import scala.collection.mutabl ...

  9. Mac Pro 安装 cmake,报错 Warning: cmake-3.5.2 already installed, it's just not linked

    1.先安装 brew,参考文章:Mac Pro 安装 Homebrew 软件包管理工具 2.执行安装命令 brew install cmake 出现警告提示: Warning: cmake-3.5.2 ...

  10. JAVA-多屏幕显示

    以下代码适用于:一台主机连接多台显示器,JAVA Swing窗口需要分别显示到对应的显示器上. GraphicsEnvironment env = GraphicsEnvironment.getLoc ...