044 Wildcard Matching 通配符匹配
实现一个支持 '?' 和 '*' 的通配符匹配。
'?' 匹配任何单个字符。
'*' 匹配任何数量的字符 (包括0个)。
匹配应覆盖 整个 输入字符串(而不是部分)。
这个函数原型为:
bool isMatch(const char *s, const char *p)
示例:
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://leetcode.com/problems/wildcard-matching/description/
class Solution {
public:
bool isMatch(string s, string p) {
int m=s.size();
int n=p.size();
vector<vector<int>> dp(m+1,vector<int>(n+1));
dp[0][0]=true;
for(int i=1;i<=n;++i)
{
if(p[i-1]=='*')
{
dp[0][i]=dp[0][i-1];
}
}
for(int i=1;i<=m;++i)
{
for(int j=1;j<=n;++j)
{
if(p[j-1]=='*')
{
dp[i][j]=dp[i-1][j]||dp[i][j-1];
}
else
{
dp[i][j]=(s[i-1]==p[j-1]||p[j-1]=='?')&&dp[i-1][j-1];
}
}
}
return dp[m][n];
}
};
参考:https://www.cnblogs.com/grandyang/p/4401196.html
044 Wildcard Matching 通配符匹配的更多相关文章
- [Leetcode] Wildcard matching 通配符匹配
Implement wildcard pattern matching with support for'?'and'*'. '?' Matches any single character. '*' ...
- [LeetCode]Wildcard Matching 通配符匹配(贪心)
一開始採用递归写.TLE. class Solution { public: bool flag; int n,m; void dfs(int id0,const char *s,int id1,co ...
- [LeetCode] Wildcard Matching 外卡匹配
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- [LeetCode] 44. Wildcard Matching 外卡匹配
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...
- LeetCode 044 Wildcard Matching
题目要求:Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches ...
- Java for LeetCode 044 Wildcard Matching
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- [Swift]LeetCode44. 通配符匹配 | Wildcard Matching
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...
- [LeetCode][Facebook面试题] 通配符匹配和正则表达式匹配,题 Wildcard Matching
开篇 通常的匹配分为两类,一种是正则表达式匹配,pattern包含一些关键字,比如'*'的用法是紧跟在pattern的某个字符后,表示这个字符可以出现任意多次(包括0次). 另一种是通配符匹配,我们在 ...
- LeetCode 44. 通配符匹配(Wildcard Matching)
题目描述 给定一个字符串 (s) 和一个字符模式 (p) ,实现一个支持 '?' 和 '*' 的通配符匹配. '?' 可以匹配任何单个字符. '*' 可以匹配任意字符串(包括空字符串). 两个字符串完 ...
随机推荐
- Java丨springMVC + Ajax 来进行分页
javaweb 分页技术 实现的方式有很多种,但是小编在网上问了度娘也问了谷老师,得到的答案让小编我不是很满意,so,还是自己写吧! 在小编的博文中,小编不会上传源码,只会把重要的代码和思路供大家参考 ...
- (转)C++经典面试题(最全,面中率最高)
1.new.delete.malloc.free关系 delete会调用对象的析构函数,和new对应free只会释放内存,new调用构造函数.malloc与free是C++/C语言的标准库函数,new ...
- C语言中mktime函数功能及用法
今天联系写一个日历的程序,需要算出月份中的第一天是星期几,用到了mktime()这个函数,感觉这个函数挺有用的,分享给大家. 原型:time_t mktime(struct tm *) 其中的tm结构 ...
- linux 进程学习笔记-named pipe (FIFO)命名管道
与“无名管道”不同的是,FIFO拥有一个名称来标志它,所谓的名称实际上就是一个路径,比如“/tmp/my_fifo”,其对应到磁盘上的一个管道文件,如果我们用file命令来查看其文件类型的话,会得到如 ...
- bzoj 2865 字符串识别 —— 后缀数组
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2865 唯一出现的子串就是每个后缀除去和别的后缀最长的 LCP 之外的前缀: 所以用这个更新一 ...
- 洛谷 P4238 [模板] 多项式求逆
题目:https://www.luogu.org/problemnew/show/P4238 看博客:https://www.cnblogs.com/xiefengze1/p/9107752.html ...
- JSOI2008星球大战——联通块数量
题目:https://www.luogu.org/problemnew/show/1197 此题不能按时间顺序进行删点.求连通块数量,而应打破时间的思维,先形成一张没有要删去的点的图,再从后往前逐个加 ...
- Python3解leetcode Valid Parentheses
问题描述: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if th ...
- => in Scala
What does => mean in Scala? 操作符=>在Scala中什么意思? 百度了下,有个论坛给出了比较全面的回答,请参见http://stackoverflow.com/ ...
- Robot FrameWork基础学习(二)
在Robot Framework中,测试套件(Test Suite)主要是存放测试案例,而资源文件(Resource)就是用来存放用户关键字. 内部资源:Resource 外部资源: External ...