【leetcode】Wildcard Matching(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
我的思路:不提了,太挫了,写了100多行代码都没搞定,直接看大神10行搞定的代码吧:
其实主要的问题就在于p中的*究竟代表哪几个字母,大神的代码中用ss记录*代表字母的后面一个位置,star记录p中*的位置。
先假设*代表0个字符,如果后面发现不成立,再返回来
设*代表1个字符,............................
bool isMatch(const char *s, const char *p) {
const char* star=NULL;
const char* ss=s;
while (*s){
//advancing both pointers when (both characters match) or ('?' found in pattern)
//note that *p will not advance beyond its length
if ((*p=='?')||(*p==*s)){s++;p++;continue;}
// * found in pattern, track index of *, only advancing pattern pointer
if (*p=='*'){star=p++; ss=s;continue;}
//current characters didn't match, last pattern pointer was *, current pattern pointer is not *
//only advancing pattern pointer
if (star){ p = star+; s=++ss;continue;}
//current pattern pointer is not star, last patter pointer was not *
//characters do not match
return false;
}
//check for remaining characters in pattern
while (*p=='*'){p++;}
return !*p;
}
【leetcode】Wildcard Matching(hard) ★ 大神太牛了的更多相关文章
- 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
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 ...
- [LeetCode] Wildcard Matching 字符串匹配,kmp,回溯,dp
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- [Leetcode] Wildcard matching 通配符匹配
Implement wildcard pattern matching with support for'?'and'*'. '?' Matches any single character. '*' ...
- leetcode Wildcard Matching greedy algrithm
The recursive program will result in TLE like this: class Solution { public: bool isMatch(const char ...
- [LeetCode]Wildcard Matching 通配符匹配(贪心)
一開始採用递归写.TLE. class Solution { public: bool flag; int n,m; void dfs(int id0,const char *s,int id1,co ...
随机推荐
- 解决pydev报unsolved import的问题
安装Flask_RESTful-0.2.11包后, 并在pydev 对应的 interpreter 重新刷新了System PYTHONPATH, 看见Lib\site-packages\flask_ ...
- WCF--提示:"未找到终结点。"
刚开始调用WCF的时候一直报错... ““System.ServiceModel.EndpointNotFoundException”类型的异常在 mscorlib.dll 中发生,但未在用户代码中进 ...
- IOC和bean容器
- 使用 IntelliJ IDEA 导入 Spark源码及编译 Spark 源代码
1. 准备工作 首先你的系统中需要安装了 JDK 1.6+,并且安装了 Scala.之后下载最新版的 IntelliJ IDEA 后,首先安装(第一次打开会推荐你安装)Scala 插件,相关方法就不多 ...
- Eclipse中集成Ant配置 (转)
目前的Eclipse都集成了ant,本文图示如何在eclipse下使用ant. 1.新建Java Project-新建Java文件HelloWorld.java HelloWorld.java pac ...
- APPCAN MAS接口之SOAP
APPCAN MAS接口中使用webservice接口形式,示例代码如下: 1 var MEAP=require("meap"); 2 3 function run(Par ...
- oracle 彻底删除用户及表空间
1.删除表空间 可以先将其offline alter tablespace xxx offline; 将磁盘上的数据文件一同删除 drop tablespace xxx including conte ...
- View和ViewImage设置图片
1.view类的设置背景android:background --setBackgroundResource(int) --A drawable to use as the background. s ...
- Java File 常用操作回顾
最近项目中要用到File这个类,温故而知新,回过头来回顾下这个File类,File类主要是对磁盘目录,文件进行操作的Api,具体其实查JDK api 的File全能获取到. 下面写一些文件目录的基本操 ...
- Java 7 Concurrency Cookbook 翻译 序言
在日常的Java代码开发过程中,很难免地有对多线程的需求,掌握java多线程和并发的机制也是Java程序员写出更健壮和高效代码的基础.笔者找寻国内已出版的关于Java多线程和并发的的中文书籍和翻译书籍 ...