【LeetCode】44. Wildcard Matching (2 solutions)
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
解法一:
这题想法参考了https://oj.leetcode.com/discuss/10133/linear-runtime-and-constant-space-solution,
类似于一种有限状态机的做法。
主要思想如下:
由于*匹配多少个字符是不一定的,于是首先假设*不匹配任何字符。
当后续匹配过程中出错,采用回溯的方法,假设*匹配0个字符、1个字符、2个字符……i个字符,然后继续匹配。
因此s需要有一个spos指针,记录当p中的*匹配i个字符后,s的重新开始位置。
p也需要一个starpos指针指向*的位置,当回溯过后重新开始时,从starpos的下一位开始。
class Solution {
public:
bool isMatch(const char *s, const char *p) {
char* starpos = NULL;
char* spos = NULL; while(true)
{
if(*s == && *p == )
//match all
return true;
else if(*s == && *p != )
{//successive *p must be all '*'
while(*p != )
{
if(*p != '*')
return false;
p ++;
}
return true;
}
else if(*s != && *p == )
{
if(starpos != NULL)
{//maybe '*' matches too few chars in s
p = starpos + ;
s = spos + ;
spos ++; //let '*' matches one more chars in s
}
else
//*s is too long
return false;
}
else
{
if(*p == '?' || *s == *p)
{
s ++;
p ++;
}
else if(*p == '*')
{
starpos = (char*)p;
spos = (char*)s;
p ++;
//start successive matching from "'*' matches non"
}
//currently not match
else if(starpos != NULL)
{//maybe '*' matches too few chars in s
p = starpos + ;
s = spos + ;
spos ++; //let '*' matches one more chars in s
}
else
//not match
return false;
}
}
}
};
解法二:
模仿Regular Expression Matching的递归做法,小数据集上能过。
当*数目太多时会超时。
class Solution {
public:
bool isMatch(const char *s, const char *p) {
if(*p == )
return (*s == );
if(*p == '*')
{
//case1: *p matches 0 chars in s
if(isMatch(s, p+))
return true; //case2: try all possible numbers
while(*s != )
{
s ++;
if(isMatch(s, p+))
return true;
}
return false;
}
else
{
if((*s==*p) || (*p=='?'))
return isMatch(s+, p+);
else
return false;
}
}
};
以下是我的测试代码,小数据上全部通过:
int main()
{
Solution s;
cout << s.isMatch("aa","a") << endl;
cout << s.isMatch("aa","aa") << endl;
cout << s.isMatch("aaa","aa") << endl;
cout << s.isMatch("aa","*") << endl;
cout << s.isMatch("aa","a*") << endl;
cout << s.isMatch("ab","?*") << endl;
cout << s.isMatch("aab","c*a*b") << endl;
return ;
}
【LeetCode】44. Wildcard Matching (2 solutions)的更多相关文章
- 【leetcode】44. Wildcard Matching
题目如下: 解题思路:本题和[leetcode]97. Interleaving String非常相似,同样可以采用动态规划的方法.记dp[i][j] = 1或者0 表示pattern[0:i]是否匹 ...
- 【一天一道LeetCode】#44. Wildcard Matching
一天一道LeetCode系列 (一)题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matches a ...
- 【leetcode】Regular Expression Matching (hard) ★
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【LeetCode】75. Sort Colors (3 solutions)
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 【LeetCode】90. Subsets II (2 solutions)
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 【leetcode】Regular Expression Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
- 【LeetCode】28. Implement strStr() (2 solutions)
Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haysta ...
- 【LeetCode】130. Surrounded Regions (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- 【LeetCode】1023. Camelcase Matching 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则+字典 日期 题目地址:https://leet ...
随机推荐
- 3D屏保:魔方2.0版本
一个三维魔方的屏保软件,可支持2级到72级的魔方.启动后魔方会自动旋转,并最终回到初始状态.有很多人问我这是怎么做到的,用的什么解魔方的算法,其实我自己根本就不会玩魔方,别人用技巧解魔方,我这程序中用 ...
- IOS学习笔记02---语言发展概述,计算机语言简介.
IOS学习笔记02---语言发展概述,计算机语言简介. ------------------------------------------------------------------------ ...
- Evaluate Reverse Polish Notation leetcode java
题目: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are + ...
- Rotate List leetcode java
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example: Gi ...
- WebViewClient 简介 API 案例
代码位置:https://github.com/baiqiantao/WebViewTest.git 设计思想理解 在WebView的设计中,不是什么事都要WebView类干的,有相当多的杂事是分给其 ...
- python - 增强的格式化字符串format函数
语法 它通过{}和:来代替%. “映射”示例 通过位置 In [1]: '{0},{1}'.format('kzc',18) Out[1]: 'kzc,18' In [2]: '{},{}'.form ...
- IOS UITableView索引排序功能
UITbableView分组展示信息时,有时在右侧会带索引,右侧的索引一般为分组的首字母,比如城市列表的展示.当点击右侧索引的字母,列表会快速跳到索引对应的分组,方便我们快速查找.下面,就介绍一下索引 ...
- cocos2dx游戏存储举例及其注意事项
今天白白跟大家分享一下cocos2dx中游戏的存储及需要注意的事项 cocos2dx中自带了存储类:CCUserDefault ,倘若需要存储的数据量教大的话,建议使用数据库来存储 现在先给大家看一下 ...
- IE8中伪元素动态作用样式不重绘bug记录
前阵子对公司框架的前端优化中,使用了字体图标(iconfont)来做模块的图标集,供用户进行配置选择. 字体图标的有非常好的灵活性和复用性,可以像处理文字一样通过font-size进行大小设置.通过c ...
- 体绘制(Volume Rendering)概述之4:光线投射算法(Ray Casting)实现流程和代码(基于CPU的实现)
转自:http://blog.csdn.net/liu_lin_xm/article/details/4850630 摘抄“GPU Programming And Cg Language Primer ...