实现一个支持 '?' 和 '*' 的通配符匹配。
'?' 匹配任何单个字符。
'*' 匹配任何数量的字符 (包括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. linux 多线程编程-读写者问题

    #include <cstdio> #include <pthread.h> #include <unistd.h> ]; int i,j; pthread_rwl ...

  2. Android 在Activity中对SQLite的操作

    注册 package com.scme.ui; import android.app.Activity; import android.content.Intent; import android.o ...

  3. LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List

    原题链接在这里:https://leetcode.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/ 题目: C ...

  4. 【Lintcode】122.Largest Rectangle in Histogram

    题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...

  5. vue之axios+php+mysql

    博主原创,未经许可请勿转载 哦 1.axios配置请看上篇 2.mysql数据库编写,表名为area_list 3.json.php文件在notebeans中编写 <?php header('C ...

  6. MySQL5.6中新增特性、不推荐使用的功能以及废弃的功能

    虽然已经使用MySQL5.6版本有一段时间了,但由于没有和之前的版本作过详细比较,所以对于哪些重要的或者不太重要的特性是在新版本中引入的,还有哪些特性已经或者将要从旧版本中移除的并没有一个十分全面的了 ...

  7. IOS要用到的零碎东西

    有些东西虽然不重要,但是零零碎碎会用到,就做个笔录吧: 协议中有2个关键字可以控制方法是否要实现(默认是@required),在大多数情况下, 用途在于程序员之间的交流 @required:这个方法必 ...

  8. 【240】◀▶IEW-Unit05

    Unit 5 Education: Study Abroad 表格技巧讲解 1. Model1对应表格分析 This table shows the numbers of international ...

  9. UVaLive 4727 Jump (递推)

    题意:约瑟夫环,求最后三个数. 析:f[i] = (f[i-1] + k) % i 这是求最后一个数时候,我们倒着推到第一个数时,只有一个数,所以当只有两个数时,就是另一数, 同理,我们可以求得第三个 ...

  10. HDU - 6344 2018百度之星资格赛 1001调查问卷(状压dp)

    调查问卷  Accepts: 1289  Submissions: 5642  Time Limit: 6500/6000 MS (Java/Others)  Memory Limit: 262144 ...