实现一个支持 '?' 和 '*' 的通配符匹配。
'?' 匹配任何单个字符。
'*' 匹配任何数量的字符 (包括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 通配符匹配的更多相关文章

  1. [Leetcode] Wildcard matching 通配符匹配

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

  2. [LeetCode]Wildcard Matching 通配符匹配(贪心)

    一開始採用递归写.TLE. class Solution { public: bool flag; int n,m; void dfs(int id0,const char *s,int id1,co ...

  3. [LeetCode] Wildcard Matching 外卡匹配

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

  4. [LeetCode] 44. Wildcard Matching 外卡匹配

    Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...

  5. LeetCode 044 Wildcard Matching

    题目要求:Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches ...

  6. Java for LeetCode 044 Wildcard Matching

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

  7. [Swift]LeetCode44. 通配符匹配 | Wildcard Matching

    Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...

  8. [LeetCode][Facebook面试题] 通配符匹配和正则表达式匹配,题 Wildcard Matching

    开篇 通常的匹配分为两类,一种是正则表达式匹配,pattern包含一些关键字,比如'*'的用法是紧跟在pattern的某个字符后,表示这个字符可以出现任意多次(包括0次). 另一种是通配符匹配,我们在 ...

  9. LeetCode 44. 通配符匹配(Wildcard Matching)

    题目描述 给定一个字符串 (s) 和一个字符模式 (p) ,实现一个支持 '?' 和 '*' 的通配符匹配. '?' 可以匹配任何单个字符. '*' 可以匹配任意字符串(包括空字符串). 两个字符串完 ...

随机推荐

  1. Python实现结对编程项目

    Github (李昆乘)(陈俊豪) 开发流程 PSP2.1 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Planning 计划 ...

  2. swoole_table

    Memory Swoole提供了7个内存操作的模块,在多进程编程中可以帮助开发者实现一些特殊的需求. swoole_table 是基于共享内存和锁实现的超高性能,并发数据结构

  3. 【坑坑坑坑坑】fwrite没有把数据写到文件中???

    原文:https://blog.csdn.net/kuaidfkuai/article/details/45918025 <unix环境高级编程>中介绍标准IO: 标准IO流操作读写普通文 ...

  4. composer镜像安装laravel

    博主最近在学习Laravel的框架的相关知识,对于Laravel的许多新特性,大家最好还是去查看官网文档最好,Laravel的文档非常完善,中文英文的都有,可以很好的解决你的困惑. 但是我们会发现学习 ...

  5. 如何将 Python 程序打包成 .exe 文件?

    有不少订阅本公众号的朋友都不是玩 Python,甚至都不是计算机相关专业的,当我给他们一个 Python 程序时,他们是完全不知道该怎么运行的. 于是我想是不是可以将我的程序打包成可执行文件,直接运行 ...

  6. 自己实现的vector

    #include <iostream> #include <memory> using std::cout; using std::endl; using std::alloc ...

  7. 基于zookeeper的MySQL主主负载均衡的简单实现

    1.先上原理图 2.说明 两个mysql采用主主同步的方式进行部署. 在安装mysql的服务器上安装客户端(目前是这么做,以后想在zookeeper扩展集成),客户端实时监控mysql应用的可用性,可 ...

  8. unix-like 图形服务组件(ubuntu)

    ubuntu18.04 准备回归wayland, 因为手机平板的ubuntu无人使用,开发成本太高.所以弃用Unity8, 重新改用 waylang 的GNOME sudo systemctl dis ...

  9. openstack开发环境搭建

    1       目的 让linux下的openstack代码能在windows上面实现同步开发. 2       目标 使用samba实现window与Linux的文件共享. 3       实验环境 ...

  10. => in Scala

    What does => mean in Scala? 操作符=>在Scala中什么意思? 百度了下,有个论坛给出了比较全面的回答,请参见http://stackoverflow.com/ ...