【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 ...
随机推荐
- 深度学习数据集Deep Learning Datasets
Datasets These datasets can be used for benchmarking deep learning algorithms: Symbolic Music Datase ...
- 10 Big Data Possibilities for 2017 Based on Oracle's Predictions
2017 will see a host of informed predictions, lower costs, and even business-centric gains, courtesy ...
- scala编程第16章学习笔记(4)——List对象的方法
通过元素创建列表:List.apply List(1, 2, 3) 等价于List.apply(1, 2, 3): scala> List.apply(1, 2, 3) res0: List[I ...
- RabbitMQ Zabbix 监控
RabbitMQ Zabbix 监控 参考: https://github.com/jasonmcintosh/rabbitmq-zabbix copy api.py list_rabbit_node ...
- vue-router 懒加载优化
一.路由懒加载 1.先安装 babel 动态引入插件 npm install --save-dev babel-plugin-syntax-dynamic-import 2.修改router/inde ...
- what difference between libfm and libffm
https://www.kaggle.com/users/25112/steffen-rendle/forum Congratulations to Yu-Chin, Wei-Sheng, Yong ...
- C++类型转换的经典例子
- HTML5游戏,五子棋
在线演示 本地下载 最近html5的游戏还真是不少,这种在线游戏既简单又有趣.收藏几个在午休时间娱乐一下.何乐而不为呢?喜欢研究的可以下载代码看看.超级推荐!
- Inception in CNN
之前也写过GoogLeNet的笔记.但那个时候对Inception有些似懂非懂,这周又一次看了一遍,觉得有了新的体会,特地又一次写一篇博客与它再续前缘. 本文属于论文笔记性质.特此声明. Networ ...
- 编程算法 - 二叉搜索树 与 双向链表 代码(C++)
二叉搜索树 与 双向链表 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 题目:输入一颗二叉搜索树, 将该二叉搜索树转换成一个排序的双向链表. 要求 ...