LeetCode——044
来源:互联网 发布:淘宝免费推广的方法 编辑:程序博客网 时间:2024/05/01 05:35
/*
44. Wildcard Matching My Submissions QuestionEditorial Solution
Total Accepted: 55229 Total Submissions: 317656 Difficulty: Hard
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
Subscribe to see which companies asked this question
*/
/*
解题思路:
这道题通配符匹配问题还是小有难度的,这道里用了贪婪算法Greedy Alogrithm来解,由于有特殊字符和?,其中?能代替任何字符,能代替任何字符串,那么我们需要定义几个额外的指针,其中scur和pcur分别指向当前遍历到的字符,再定义pstar指向p中最后一个*的位置,sstar指向此时对应的s的位置,具体算法如下:
定义scur, pcur, sstar, pstar
如果*scur存在
如果*scur等于*pcur或者*pcur为 ‘?’,则scur和pcur都自增1
如果pcur为’‘,则pstar指向pcur位置,pcur自增1,且sstar指向scur
如果pstar存在,则pcur指向pstar的下一个位置,scur指向sstar自增1后的位置
如果pcur为’*’,则pcur自增1
若*pcur存在,返回False,若不存在,返回True
*/
class Solution {public: bool isMatch(string s, string p) { const char*scur=s.c_str(),*pcur=p.c_str(),*sstar=NULL,*pstar=NULL; while(*scur){ if(*scur==*pcur|| *pcur=='?'){ ++scur; ++pcur; }else if(*pcur=='*'){ pstar = pcur++; sstar=scur; }else if(pstar){ pcur=pstar+1; scur=++sstar; }else return false; } while(*pcur=='*')++pcur; return !*pcur; }};
- LeetCode——044
- leetcode 044 —— Wildcard Matching
- 开始刷刷Leetcode—leetcode第二题
- LeetCode —— Combinations
- LeetCode —— Triangle
- LeetCode —— Anagrams
- [leetcode] Subsets——@
- Permutations——LeetCode
- minimum_window_substring ——leetcode
- LeetCode——Candy
- LeetCode——Triangle
- LeetCode——Combinations
- LeetCode——Permutations
- *candy——leetcode
- gas_station——leetcode
- LeetCode——Candy
- LeetCode——Triangle
- LeetCode——Anagrams
- PHP--extract 从数组中将变量导入到当前的符号表
- Fragment Menu and Navigate
- C++中的模板那点事
- 大学毕业4年-回顾和总结(7)-全局观
- 第八周项目一(2) 数组做数据成员(工资类 数组 文件)
- LeetCode——044
- Spark性能相关参数配置 之 压缩和序列化相关
- Linux上使用iptables设置防火墙
- 硬件切换485电路
- IOS-UI设置字体属性
- js keyup、keypress和keydown事件 详解
- 陶哲轩实分析 3.3 节习题试解
- String.format()用法
- 【EJB系列】(四)——EJB基本内容总结