算法第15周Wildcard Matching[hard]
来源:互联网 发布:网络考试系统破解版 编辑:程序博客网 时间:2024/06/15 18:59
Description
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") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "*") → trueisMatch("aa", "a*") → trueisMatch("ab", "?*") → trueisMatch("aab", "c*a*b") → false
Analysis
这道题目是进行字符串匹配。*符号可以匹配任意的字符串(包括空字符串),?符号可以匹配任意字符。
我们从左到右依次扫描字符串和模板。如果遇到‘*’号,我们需要记录下此时所处的字符串位置istar和模板位置jstar,并将模板移后一位继续进行匹配。一旦遇到不匹配的情况(p[j] != s[i] && p[j] != '?'),我们考虑之前是否遇到'*'号,如果遇到那我们把遇到‘*’号到现在所匹配的符号都认为是与'*'进行匹配成功的,这是我们需要将j移到遇到‘*’号时位置,继续进行匹配。最后还需检查是否扫描完模板。
Solution
class Solution {public: bool isMatch(string s, string p) { int m = s.length(); int n = p.length(); int i, j; int istar, jstar; istar = -1; jstar = -1; for (i = 0, j = 0; i < m;) { if (p[j] == '*') { istar = i; jstar = j; j++; } else { if (p[j] != s[i] && p[j] != '?') { if (istar >= 0) { i = istar++; j = jstar; i++; } else { return false; } } else { i++; j++; } } } while (p[j] == '*') j++; if (j == n) return true; return false; }};
阅读全文
0 0
- 算法第15周Wildcard Matching[hard]
- (算法分析Week9)Wildcard Matching[Hard]
- 44、 Wildcard Matching (Hard)
- 算法第12周Regular Expression Matching[hard]
- Hard-题目51:44. Wildcard Matching
- Leetcode 44. Wildcard Matching (Hard) (cpp)
- LeetCode算法题目: Wildcard Matching
- 【算法】【Dynamic Programming】Wildcard Matching
- LeetCode第44题之 Wildcard Matching
- 每日算法之三十五:Wildcard Matching
- 【leetcode 44】 Wildcard Matching 【正确算法】
- 算法分析课每周练习 Wildcard Matching
- LeetCode算法题——Wildcard Matching
- 算法练习(17):Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Android基础知识四(View类和ViewGroup类、View绘制流程机制)
- post请求爬取艺龙酒店的评论
- VirtualBox实现宿主机和虚拟机之间网络的通讯
- [other]电脑知识
- Mysql的正则匹配与常用匹配符_Mysql必知必会学习记录01
- 算法第15周Wildcard Matching[hard]
- Leetcode 740. Delete and Earn
- C++primer学习 12.17
- 机房重构之退卡——触发器增强版
- 02nodeJS中使用的模块化规范commonJS
- myeclipse的com.mysql.jbdc.Driver异常和java.lang.NullPointerException异常解决方法
- Python实现--贪心算法与活动安排问题
- 制作一个购物车函数
- 关于OSG 帮助文档。