[Leetcode] 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
这题和正则表达式匹配有点类似,' . '和本题中的' ? '一样,区别在于' * ',在正则表达式那题中,' * '可以表示之前字符的0个或多个;本题中,可以表示任意字符串。好吧,做了,第一次做的时候,没有完全搞懂,这次出问题了,又去网上找帮助了。本文章参考了peerlessbloom 、点缀星辰、Grandyang。
思路:当*s ==*p || *p=='? '时,对应的指针分别++即可;当遇到字符' * '时,该怎么办呢?是否像正则表达式那题中的思路把 *s和*(p+1)去比较了?若是这样的,说明没有理解清楚:题目中' * '可以表示任意字符串的含义,举个例子来说明s=" aacabacd "和 p=" a*acd ",这种情况下,若是按上题中的思想,则要返回false,但是本题这种情况确是匹配的。这题中,' * '和' acab '匹配了。这如何实现的了?我们先看*号匹配的几种情况(例子来自 点缀星辰的博客,这里只是结合自己的语言给出了):
1)*号匹配了串 s 中间指定点秩后所有的字符。这里*号匹配了“bcac”。
2)*号匹配部分串的情况。这里*号匹配了“bc”。这种情况就是字符串p中的*号之后的字符' a '和*号对应的S中的字符' b '去匹配,若不匹配,则比较s 中的下一个,直到匹配。
那是否所有的情况就结束了?如果是这样的,就和正则表达式的那种情况,很相似了,好吧,我当时就是这么想的,错了。还有其他的情况,如:
3)如下图1,刚开始,*号的下一个和*号对应的S中的字符' a '匹配,这意味着*号代表0个字符,直到' d '和 ' b '不匹配,那这种情况,怎么办了?是否返回false?这种情况显然不会返回false,因为,图中绿色的部分是匹配的,这也说明了一个问题,就是*号代表0个字符这种形式是不对了。那该如何?接下来应该如图2所示,p中字符a跟s中字符c 对比,不匹配,再跟s中字符b对比,直到在s中遇到字符a,即匹配,这样,p和s同时的比较下一个字符。若后面还有*号,能遍历到*号,说明*号之前都是匹配的,这样只要重复上述过程即可。
图1
图2
这就遇到一个问题了?当匹配了若干字符之后,才发现不匹配,如情况3),这时如何像上述过程那样,比较p中*号的下一位和s中*号对应的那位的下一位了?这时,我们采取的方法是,当遇到*号时,我们将使用两指针记录下此时的值,若在以后的对比中,遇到不匹配的情况,即可返回。当然,p中*号的位置是相对不变的(直到遇到下一*号),而s中用来的对比字符是在变化的。代码如下:
- class Solution {
- public:
- bool isMatch(const char *s, const char *p)
- {
- const char *preS=NULL; //用来记录位置
- const char *preP=NULL;
- while(*s)
- {
- if(*p=='?' || *p==*s)
- {
- p++;
- s++;
- }
- else if(*p=='*')
- {
- preS=s;
- preP=p++;
- }
- else if(preP) //后面遇到不匹配的情况,则返回重新比较情况3)
- {
- p=preP+;
- s=++preS;
- }
- else
- return false;
- }
- while(*p=='*')
- p++;
- return !*p;
- }
- };
[Leetcode] Wildcard matching 通配符匹配的更多相关文章
- [LeetCode]Wildcard Matching 通配符匹配(贪心)
一開始採用递归写.TLE. class Solution { public: bool flag; int n,m; void dfs(int id0,const char *s,int id1,co ...
- [LeetCode] Wildcard Matching 外卡匹配
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- 044 Wildcard Matching 通配符匹配
实现一个支持 '?' 和 '*' 的通配符匹配.'?' 匹配任何单个字符.'*' 匹配任何数量的字符 (包括0个).匹配应覆盖 整个 输入字符串(而不是部分).这个函数原型为:bool isMatch ...
- [LeetCode] 44. Wildcard Matching 外卡匹配
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...
- LeetCode: Wildcard Matching 解题报告
Wildcard MatchingImplement wildcard pattern matching with support for '?' and '*'. '?' Matches any s ...
- [LeetCode] Wildcard Matching 题解
6. Wildcard Matching 题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matche ...
- [Leetcode] Wildcard Matching
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- [LeetCode] Wildcard Matching 字符串匹配,kmp,回溯,dp
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- [leetcode]Wildcard Matching @ Python
原题地址:https://oj.leetcode.com/problems/wildcard-matching/ 题意: Implement wildcard pattern matching wit ...
随机推荐
- over开窗函数的用法
over(partition by c1.pmid,d1.type,e1.objid order by e1.objid ) pinum 先根据字段排序,pinum.在取第一条数据and p1.pi ...
- css公共类
/*iOS弹性滚动*/ .scrolling{ position: absolute; width: 100%; height:100%; overflow-x:hidden; overflow-y: ...
- 三、css篇
#这里强烈推荐一本书<css世界>,css第一书. #上面的层叠顺序得记住. 1.align-items justify-content 是flex(弹性盒模型)必须要会的属性,alig ...
- MVC模型与MTV模型
MVC模型: MVC(Model View Controller 模型-视图-控制器)是一种Web架构的模式,它把业务逻辑.模型数据.用户界面分离开来,让开发者将数据与表现解耦,前端工程师可以只改页面 ...
- ALVのイベントを取得する方法
概要 表示されたALVをダブルクリックした時に別画面へ遷移する方法を説明しよう.下記サンプルのように標準トランザクションへ遷移したり.別のALVを表示したりする事が可能である. サンプルコード ABA ...
- 9.Mongodb与python交互
1.与python交互 点击查看官方文档 安装python包 进入虚拟环境 sudo pip install pymongo 或源码安装 python setup.py 引入包pymongo impo ...
- mysql 大数据分页查询优化
应用场景: 当有一张表的数据非常大,需要使用到分页查询,分页查询在100w条后查询效率非常低: 解决方案: 1.业务层解决:只允许用户翻页一百页以内,十条一页: 2.使用where id > 5 ...
- TFTP & commons-net-3.3.jar
项目需求:上传文件到服务器,TFTP 了解TFTP http://wenku.baidu.com/link?url=MhRVgIySotFMkm5ar6B71zROPMoqC7cd5cSbKJo2kx ...
- cf#513 B. Maximum Sum of Digits
B. Maximum Sum of Digits time limit per test 2 seconds memory limit per test 512 megabytes input sta ...
- CSP201604-1:折点计数
引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...