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 ...
随机推荐
- Java学习的第二十七天
1.内存操作流 缓冲流 用BufferedReader读取数据 使用BufferedWriter写数据 使用BufferInputStream和BufferOutputStream读写图片 2.有很多 ...
- Learn day10 锁
1.锁 # ### 锁 from multiprocessing import Lock,Process import json,time """ # 创建一把锁 loc ...
- rpm包的卸载与安装
1. rpm包的管理介绍:一种用于互联网下载包的打包及安装工具,它包含在某些Linux分发版中,它生成具有RPM扩展名的文件,RPM是RedHat Package Manager(RedHat软件包管 ...
- Lte Design Documentation之RRC
RRC 特点 RRC模型在模拟器中提供以下功能 生成(在eNB中)和解释(在UE中)信息块(尤其是MIB和SIB1, SIB2) 初始化小区选择 RRC连接建立过程 RRC重新配置程序, 支持以下方式 ...
- python爬虫00什么是爬虫
用一个自动化的程序把网站背后的程序爬取下来. 在互联网上许许多多的网站,他们都是托管在服务器上的,这些服务器24小时运行着,刻刻 等待着别人的请求.所以,爬虫首先会模拟请求,就好像你在浏览器输入网址, ...
- 企业网络拓扑RSTP功能实例
组网图形 RSTP简介 以太网交换网络中为了进行链路备份,提高网络可靠性,通常会使用冗余链路.但是使用冗余链路会在交换网络上产生环路,引发广播风暴以及MAC地址表不稳定等故障现象,从而导致用户通信质 ...
- Effective Modern C++ ——条款5 优先选择auto,而非显式型别声明
条款5 对于auto ,他的好处不仅仅是少打一些字这么简单. 首先在声明的时候, 使用auto会让我们养成初始化的习惯: auto x;//编译不通过必须初始化. 再次对于auto而言,它可以让我们定 ...
- nginx&http 第三章 ngx http 框架处理流程
1. nginx 连接结构 ngx_connection_t 这个连接表示是客户端主动发起的.Nginx服务器被动接受的TCP连接,我们可以简单称其为被动连接.同时,在有些请求的处理过程中,Nginx ...
- docker生产——容器通信
简介 在接触docker的第一天起,大家应该就知道:docker容器使用沙箱机制,相互之间没有接口,一般情况下内部访问通过IP+端口.本地容器默认分配的IP极易发生变化,所以靠IP+端口访问的方式缺失 ...
- [原题复现]强网杯 2019 WEB高明的黑客
简介 原题复现: 考察知识点:python代码编写能力... 线上平台:https://buuoj.cn(北京联合大学公开的CTF平台) 榆林学院内可使用信安协会内部的CTF训练平台找到此题 简 ...