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.今天复习了第十二章的12.1的文件和12.2一直到12.2.4 2.没有问题 3.明天继续复习
- Learn day6 模块pickle\json\random\os\zipfile\面对对象(类的封装 操作 __init__)
1.模块 1.1 pickle模块 # ### pickle 序列化模块 import pickle """ 序列化: 把不能够直接存储的数据变得可存储 反序列化: 把数 ...
- MySQL图形界面客户端
图形界面客户端 使用图形界面客户端操作数据库更直观.方便.下面三个客户端都能操作MySQL,各有各自的优点. 1.Navicat Premium 下载安装包下载 关注公众号[轻松学编程],然后回复[n ...
- POJ1840 Eqs
题意描述 Eqs 求一个五元方程 \(a_1x_1^3+a_2x_2^3+a_3x_3^3+a_4x_4^3+a_5x_5^3=0\) 的解的个数. 题中给出 \(a_i\) 的值并且保证 \(-50 ...
- 一个.NET Core下的开源插件框架
插件模式历史悠久,各种中大型软件基本上都会实现插件机制,以此支持功能扩展,从开发部署层面,插件机制也可实现功能解耦,对于并行开发.项目部署.功能定制等都有比较大的优势. 在.NET Core下,一般我 ...
- Java入门(4)
阅读书目:Java入门经典(第7版) 作者:罗格斯·卡登海德 面向对象编程(OOP)将程序视为对象的集合,确定程序要完成的任务,然后将这些任务指派给最适合完成它们的对象.换言之,计算机程序是一组对象, ...
- 利用sklearn实现knn
基于上面一篇博客k-近邻利用sklearns实现knn #!/usr/bin/env python # coding: utf-8 # In[1]: import numpy as np import ...
- nginx&http 第三章 ngx http ngx_http_process_request_headers
HTTP 请求行正确处理完成后,针对 HTTP/1.0 及以上版本紧接着要做的就是请求 HEADER 的处理与解析了 /** * 用于处理http的header数据 * 请求头: * Host: lo ...
- nagle 算法 tcp nodelay 以及 quick ack分析
后面详细分析 先上传 之前总结查看源码后的总结 Nagle算法的基本定义是任意时刻,最多只能有一个未被确认的小段.所谓"小段",指的是小于MSS尺寸的数据块,所谓"未被确 ...
- 92. Reverse Linked List II 翻转链表II
Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...