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.用类函数来写时间类 import java.util.Scanner; public class Cjava { public static void main(String[]args) { T ...
- 关于Linux操作系统的文件管理与常用命令
1.显示文件内容命令:cat more less head tail cat命令 : cat命令连接文件并打印到标准输出设备上,cat经常用来显示文件的内容,类似于下的 ...
- QQ 邮箱日历提醒
偶然发现 QQ 邮箱有日历的功能,而且可以设置农历并且每年邮件 + 短信 + 微信提醒.这下重要的日子(eg:生日...)就不会忘记啦! 1.找到日历 2.历史提醒 3.新建时间 4.设置时间 5.勾 ...
- 【USACO】Strolling Cows
Strolling Cows 给定有 \(n\) 个点 \(n\) 条边的有向图,每个点的出度都为 \(1\),求图中的最大环. 显然入度为 \(0\) 的点不可能为最大环上的点,所以考虑删点. 然后 ...
- Java 中的 反射机制
概念明确 什么是类的对象? 类的对象就是:基于某个类 new 出来的对象,也称为实例对象.这个很容易理解,就不过多阐述了. 什么是类对象? 类对象就是:类加载的产物,封装了一个类的所有信息(类名.父类 ...
- PASS模型-第一周个人报告
PASS模型-第一周个人报告 博客班级 https://edu.cnblogs.com/campus/zjcsxy/SE2020 作业要求 https://edu.cnblogs.com/campus ...
- 如何在Windows Server 2012及更高版本中将域控制器降级
如何在Windows Server 2012及更高版本中将域控制器降级 如果不降级就重装系统,会出问题,所以在将域控系统重装系统之前一定要先降级. 使用服务器管理器将 Windows Server 2 ...
- 02_tcp_deadlock
# 这个程序我们是测试客户端和服务端在进行通信的过程中,可能会产生死锁的情况. # 这是因为缓冲区,和TCP协议的可靠性连接导致的. # 在程序中我们可以看到,客户端先向服务端发送数据,然后服务端就收 ...
- jackson、fastjson、kryo、protostuff等序列化工具性能对比
简介 实际项目中,我们经常需要使用序列化工具来存储和传输对象.目前用得比较多的序列化工具有:jackson.fastjson.kryo.protostuff.fst 等,本文将简单对比这几款工具序列化 ...
- KepServer与S7-1200PLC之间的OPC通信配置
对于学习上位机开发,有一种通信方式是必须要了解的,那就是OPC是OLE for Process Control的简称,然而随着技术的不断发展,人们开始对它有了新的定义,比如Open Platform ...