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. Spring MVC启动过程

    org.springframework.web.context.ContextLoaderListener ContextLoaderListener的作用就是启动Web容器时,自动装配Applica ...

  2. [FMX] Android APP 启动黑屏优化补丁

    使用说明 *************************************************** Android APP 启动黑屏优化补丁 作者: Swish, YangYxd 201 ...

  3. C-结构体、枚举

    #include <stdio.h> //结构体:可以由多个不同类型的数据构成 int main() { struct Person { //里面的3个变量,可以称为是结构体的成员或者属性 ...

  4. tmux常用命令

    tmux命令可以启动一个tmux服务,tmux服务包含多个session,session包含多个window,window包含多个pane. 常用命令tmux ls #显示已有tmux列表(C-b s ...

  5. AngularJS启动过程分析

    1111 app.controller('myCtrl',['$scope',function($scope){     $scope.wcrq=1234567890; }]); angular.bo ...

  6. php页面防重复提交方法总结

    1.提交按钮置disabled 当用户提交后,立即把按钮置为不可用状态.这种用js来实现. 提交前 复制代码 代码如下:         $("#submit").attr('di ...

  7. rational rose 顺序图的消息加数字

    主菜单——〉“Tools”——〉“Options”,在弹出的窗口中选择“Diagram”标签-->display-->sequence numbering

  8. Mastering Web Application Development with AngularJS 读书笔记(一)

    第一章笔记 (一) 一.PS:运行时配置IIS <html> <head> <script src="angular.js"></scri ...

  9. HDU 2014

    #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> typedef float ElementType; void Select_Sort ...

  10. java练手 韩信点兵

    Problem C 韩信点兵 时间限制:3000 ms  |  内存限制:65535 KB   描述 相传韩信才智过人,从不直接清点自己军队的人数,只要让士兵先后以三人一排.五人一排.七人一排地变换队 ...