题目:

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

分析:

跟第10题Regular Expression Matching很像,从正则表达式匹配变成了通配符匹配,用动态规划的方法做的话, 比之前这个题要来的简单。

还是双序列动态规划,用dp[i][j]表示s前i个与p前j个是否能匹配。

分p[j - 1]的几种情况,

  当 (p[j - 1] == s[i - i] 或者 p[j - 1] == '?')且 dp[i - 1][j - 1] == true, 则 dp[i][j] == true;

  当  p[j - 1] == '*'时, (dp[i - 1][j]  == true|| dp[i][j - 1] == true), 则 dp[i][j] == true;

初始化第一行,第一列即可。

注意: 动归的算法在leetcode上有两组样例应该是过不了的,可能还有贪心的思路可以优化,但动归的思路应该更值得学习(更有通用性),回头有时间再来补上贪心的思路。

如果要通过样例的话,可以有个小作弊,就是当s.size() > 3000时,返回false,处理掉那两个大样例。

代码:

 class Solution {
public:
bool isMatch(string s, string p) {
if (p.size() > || s.size() > ) {
return false;
}
bool dp[s.size() + ][p.size() + ] = {false};
dp[][] = true;
for (int i = ; i <= p.size(); ++i) {
dp[][i] = dp[][i - ] && (p[i - ] == '*');
}
for (int i = ; i <= s.size(); ++i) {
for (int j = ; j <= p.size(); ++j) {
if ((p[j - ] == s[i - ] || p[j - ] == '?') && dp[i - ][j - ] == true) {
dp[i][j] = true;
}
if (p[j - ] == '*' && (dp[i - ][j] || dp[i][j - ]) ){
dp[i][j] = true;
}
}
}
return dp[s.size()][p.size()];
}
};

LeetCode44 Wildcard Matching的更多相关文章

  1. 【leetcode】Wildcard Matching

    Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...

  2. LeetCode - 44. Wildcard Matching

    44. Wildcard Matching Problem's Link --------------------------------------------------------------- ...

  3. 44. Wildcard Matching

    题目: Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single charact ...

  4. [OJ] Wildcard Matching (Hard)

    LintCode 192. Wildcard Matching (Hard) LeetCode 44. Wildcard Matching (Hard) 第二次刷还是被这题虐. 其实就是跪在一个地方, ...

  5. [Leetcode][Python]44:Wildcard Matching

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 44:Wildcard Matchinghttps://oj.leetcode ...

  6. [LeetCode] Wildcard Matching 题解

    6. Wildcard Matching 题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matche ...

  7. Regular Expression Matching & Wildcard Matching

    Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...

  8. leetcode44:wildcard

    44. Wildcard Matching 问题描述 给定字符串s和模式p,判断字符串s是否完全符合模式p 其中字符串s只包含小写字母,模式串p包含小写字母.*.?,其中星号表示任意长度的任意字符串, ...

  9. 【LeetCode】44. Wildcard Matching (2 solutions)

    Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...

随机推荐

  1. cocos2d-x 知识小结(1)zorder和tag

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  2. GPIO 配置之ODR, BSRR, BRR 详解

    STM32 GPIO 配置之ODR, BSRR, BRR 详解 用stm32 的配置GPIO 来控制LED 显示状态,可用ODR,BSRR,BRR 直接来控制引脚输出状态. ODR寄存器可读可写:既能 ...

  3. USB设备不能用。提示Windows 无法启动这个硬件设备。 (代码 19)

    USB,由于其配置信息(注册表中的)不完整或已损坏, Windows 无法启动这个硬件设备. (代码 19) 原因:提示Windows 无法启动这个硬件设备. (代码 19) 处理解决方法: 1) r ...

  4. 结合windows消息系统理解C#中WndProc函数和DefWndProc函数

    Windows消息系统由3部分组成:     1.消息队列.Windows应用程序的消息是由Windows统一在一个消息队列中管理的.     2.消息循环.应用程序从Windows消息队列中获得自己 ...

  5. Linux下的Crontab应用

    crontab是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业.由于Cron 是Linux的内置服务,但它不自动起来,可以用以下的方法启动.关闭这个服务: /sbin/servic ...

  6. 不需要JAVAScript完成分页查询功能

    分页查询之前已经说过,现在用另一种方法实现,换汤不换药.但是更简单. view层代码: 控制层代码: 业务逻辑层,主要看一下方法count1()的代码: count1()方法的功能就是控制翻页,如果传 ...

  7. listView divider marginLeft marginRight

    要实现这样的效果: 新建drawable  用inset 进行实现.代码如下: <?xml version="1.0" encoding="utf-8"? ...

  8. 你尽力了么===BY cloudsky

    /////////////////////////////////////////////////////////////////////////// 这是我的同事alert7在他主页上转scz的&l ...

  9. Linux Kconfig及Makefile学习

    内核源码树的目录下都有两个文档Kconfig (2.4版本是Config.in)和Makefile.分布到各目录的Kconfig构成了一个分布式的内核配置数据库,每个Kconfig分别描述了所属目录源 ...

  10. IIS 7.0 and Web Farms

    1. IIS 6 IIS 6.0 was capable of scaling out to virtually any number of web servers and had tools lik ...