LeetCode 044 Wildcard Matching
题目要求: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
参考网址:http://blog.csdn.net/pickless/article/details/9787227
代码如下:
class Solution {
public:
bool initCheck(const char *s, const char *p) {
int l1 = 0;
int idx = 0, l2 = 0;
while (s[l1] != '\0') {
l1++;
}
while (p[idx] != '\0') {
l2 += p[idx++] != '*' ? 1 : 0;
}
return l1 >= l2;
}
bool isMatch(const char *s, const char *p) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (!initCheck(s, p)) {
return false;
}
int l = 0;
while (p[l++] != '\0');
bool prev[l], f[l];
memset(f, false, sizeof(bool) * l);
memset(prev, false, sizeof(bool) * l);
bool isFirst = true;
for (int i = 0; i < l; i++) {
if (p[i] == '*') {
f[i] = i == 0 || f[i - 1];
}
else if ((p[i] == '?' || p[i] == *s) && isFirst) {
isFirst = false;
f[i] = true;
}
else {
break;
}
}
s++;
while (*(s - 1) != '\0') {
memcpy(prev, f, l);
memset(f, false, sizeof(bool) * l);
for (int i = 0; i < l; i++) {
if (prev[i]) {
f[i] = f[i] || p[i] == '*';
f[i + 1] = f[i + 1] || p[i + 1] == '?' || p[i + 1] == *s;
}
else if (i == 0 || f[i - 1]) {
f[i] = f[i] || p[i] == '*';
}
}
s++;
}
return f[l - 1];
}
};
LeetCode 044 Wildcard Matching的更多相关文章
- Java for LeetCode 044 Wildcard Matching
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- 【leetcode】Wildcard Matching
Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...
- LeetCode - 44. Wildcard Matching
44. Wildcard Matching Problem's Link --------------------------------------------------------------- ...
- [LeetCode] 44. Wildcard Matching 外卡匹配
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...
- 【leetcode】Wildcard Matching(hard) ★ 大神太牛了
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题解-----Wildcard Matching
题目描述: '?' Matches any single character. '*' Matches any sequence of characters (including the empty ...
- LeetCode 44 Wildcard Matching(字符串匹配问题)
题目链接:https://leetcode.com/problems/wildcard-matching/?tab=Description '?' Matches any single chara ...
- 044 Wildcard Matching 通配符匹配
实现一个支持 '?' 和 '*' 的通配符匹配.'?' 匹配任何单个字符.'*' 匹配任何数量的字符 (包括0个).匹配应覆盖 整个 输入字符串(而不是部分).这个函数原型为:bool isMatch ...
随机推荐
- 团灭 LeetCode 打家劫舍问题
有读者私下问我 LeetCode 「打家劫舍」系列问题(英文版叫 House Robber)怎么做,我发现这一系列题目的点赞非常之高,是比较有代表性和技巧性的动态规划题目,今天就来聊聊这道题目. 打家 ...
- Shell 筛选符合条件的 ELF 文件
0 运行环境 本机系统:Windows 10 虚拟机软件:Oracle VM VirtualBox 6 虚拟机系统:Ubuntu 18 1 引言 - 编译过程 我们知道在 CPU 上执行的是低级别的机 ...
- maven profile filter 线上线下分开打包配置
maven自动选择不同的配置文件打包profile+filter 1. profile: [要点:] activeByDefault默认激活,不用再mvn命令时指定额外参数: [注意:] 使用非默认的 ...
- 深度学习中的Dropout
dropout是指在深度学习网络的训练过程中,对于神经网络单元,按照一定的概率将其暂时从网络中丢弃.注意是暂时,对于随机梯度下降来说,由于是随机丢弃,故而每一个mini-batch都在训练不同的网络. ...
- TCP协议原理与格式初探
目录 可靠数据传输原理 停等传输下的情况 1.经过完全可靠信道的可靠数据传输 2.经具有比特差错信道的可靠数据传输 3.经具有比特差错的丢包信道的可靠数据传输 流水线传输 1.回退N步(Go-Back ...
- maven pom.xml 报错
首先介绍背景,在eclipse中导入一个maven的项目,在我之前的电脑上导入好用,在自己的电脑上导入居然pom报错了Missing artifact junit:junit:jar:4.11,还会有 ...
- python之路《八》装饰器
装饰器是个好东西啊 那么装饰器是个什么样的东西呢,他又能做些什么呢? 1.为什么装饰器 当我们一个程序已经构建完成,并且已经发布出去了,但是现在需要增加一个活动,例如淘宝给你发送一个今日优惠,或者开启 ...
- 01、MyBatis HelloWorld
1. MyBatis简介 1)MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架 2)MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集 3)MyB ...
- RedHat Linux-配置YUM仓库
范例:配置Yum仓库 Yum软件仓库的作用是为了进一步简化RPM管理软件的难度以及自动分析所需软件包及其依赖关系的技术.可以把Yum想象成是一个硕大的软件仓库,里面保存有几乎所有常用的工具,而且只需要 ...
- C#设计模式——代理模式(Proxy Pattern)
引言 在我们的生活中,经常会遇到需要什么东西,但是自己又不是很方便或者对方不是很方便,则就需要中间的一个代理人去解决.例如代购.在软件开发中,也会遇到这样的问题.有些对象有时候会由于网络或其他的障碍, ...