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 分析:一个超时版本如下:
class Solution { public:bool isMatch(const char *s, const char *p) {if(*s == '\0') return *p == '\0';if(*p == '\0') return *s == '\0';int i = 0, j = 0;for(; *(s+i) != '\0' && *(p+j) != '\0';){if(*(s+i) == *(p+j) || *(p+j) == '?'){i++;j++;}else if(*(p+j) == '*'){while(*(s+i) != '\0'){if(isMatch(s+i, p+j+1))return true;i++;}return false;}else return false;}return *(s+i) == *(p+j);} };
迭代版本相比递归版本要快,但难度要大。这里参考leetcode.pdf的解答,用两套指针(str,ptr)和(s,p)。(str,ptr)用于探索,(s,p)用于保存探索前的状态,一遍当探索失败后恢复到原来状态。同时要注意一些细节的处理,比如多个连续的'*'。
class Solution { public:bool isMatch(const char *s, const char *p) {bool star = false;const char *str, *ptr;for(str = s, ptr = p; *str != '\0'; str++, ptr++){switch(*ptr){case '?':break;case '*':star = true;s = str, p = ptr;while(*p == '*')p++;if(*p == '\0') return true;str = s-1;ptr = p-1;break;default:if(*str != *ptr){if(!star) return false;s++;str = s-1;ptr = p-1;}}}while(*ptr == '*')ptr++;return *ptr == '\0';} };
转载于:https://www.cnblogs.com/Kai-Xing/p/4044523.html
Wildcard Matching相关推荐
- [LeetCode] Wildcard Matching 题解
6. Wildcard Matching 题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matche ...
- [LintCode] Wildcard Matching
Wildcard Matching Implement wildcard pattern matching with support for '?'and '*'. '?' Matches any s ...
- leetcode-44. Wildcard Matching
题目阐释: 正则匹配字符串,用程序实现 关键理解: 正则匹配,动态规划思想,一个个向后追溯,后面的依赖前面的匹配成功. 正则和待匹配的字符串长度不一,统一到正则字符串的index索引上,每次的字符串i ...
- [OJ] Wildcard Matching (Hard)
LintCode 192. Wildcard Matching (Hard) LeetCode 44. Wildcard Matching (Hard) 第二次刷还是被这题虐. 其实就是跪在一个地方, ...
- 【LeetCode】44. Wildcard Matching (2 solutions)
Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...
- LeetCode - 44. Wildcard Matching
44. Wildcard Matching Problem's Link --------------------------------------------------------------- ...
- 【To Understand! 重点 递归 动态规划 正则表达式匹配】LeetCode 44. Wildcard Matching
LeetCode 44. Wildcard Matching Solution1:我的答案 递归,时间复杂度是O(2n)O(2n)O(2^n)因为超时未能AC 只是记录一下,以警示后人-- class ...
- Wildcard Matching 1
Wildcard Matching 题解 题目描述 即判断字符串与正则表达式(只支持'?'和'*')是否匹配. '?'匹配任意一个字符,'*'匹配任意一个序列. 如:"abab"与 ...
- LeetCode 10. Regular Expression Matching / 44. Wildcard Matching
10. Regular Expression Matching 经典DP题目,比较复杂,需要多复习. dp[i][j] 表示 s 下标0~i,p 下标0~j 是否能够匹配 dp[i-1][j-1] ...
- LeetCode: 44. Wildcard Matching
LeetCode: 44. Wildcard Matching 题目描述 Implement wildcard pattern matching with support for '?' and '* ...
最新文章
- 【转载】MySQL Show命令总结
- SpringCloud配置中心客户端读取配置
- 「leetcode」40.组合总和II【回溯算法】详解!
- Pandas简单写入数据到csv文件
- java.io.IOException: Cannot run program “del“: CreateProcess error=2, 系统找不到指定的文件。
- 详解JavaScript中的Url编码/解码,表单提交中网址编码
- linux虚拟键盘onboard设置,求助,安装屏幕虚拟键盘onboard出错。
- js一键批量打印_(原创)javascript 实现批量打印《简历》
- 设为首页、加入收藏及保存到桌面的JS代码
- 如何高效地阅读技术类书籍与博客
- b区计算机211学校排名,考研B区院校排名
- 抖音实战~实现App端视频上传、发布、落库
- 写一篇关于乡愁的作文1000字以上
- 【docker】导入镜像报错磁盘空间不足的解决方法 【docker】修改默认的存储路径
- 目标检测算法横向比较,包括backbone、特征融合、loss、proposal/anchor/heatmap、NMS、正负样本设置等
- 安卓与“Proguard”——安卓的代码混淆
- 专业创造奇迹 彩票行业进入数据分析时代
- STM32 USB On-The-Go Host and Device Library复合设备分析
- 十七.用户注册 ---- 图形验证码 2021-04-03
- 麒麟子Cocos Creator实用技巧二:微信名字截断(支持表情)
热门文章
- poj 2376 bzoj 3389: [Usaco2004 Dec]Cleaning Shifts安排值班(贪心)
- bzoj 4956: [Wf2017]Secret Chamber at Mount Rushmore(最短路)
- 2017百度之星资格赛:1005. 寻找母串(卡特兰数+分块打表)
- Undefined function 'conv2' for input arguments of type 'double' and attributes 'full 3d complex'.
- opencv 物体形状匹配
- python 正则表达式 速查表
- 字符串的连接(复习)
- matlab2c使用c++实现matlab函数系列教程-var函数
- 卷积神经网络中的全连接层
- 雷林鹏分享:jQuery EasyUI 树形菜单 - 创建带复选框的树形菜单