【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 ...
随机推荐
- [译]我是怎么构建Node.js程序的
原文: http://blog.ragingflame.co.za/2015/4/1/how-i-build-nodejs-applications "保持简单, 保持模块化." ...
- R笔记1
gsub format > measurements<-c('3.95*3.99*2.43mm','3*3*5mm','2*2*2mm') > measurements [1] &q ...
- 【bzoj3573】[HNOI2014]米特运输
题目描述 米特是D星球上一种非常神秘的物质,蕴含着巨大的能量.在以米特为主要能源的D星上,这种米特能源的运输和储存一直是一个大问题.D星上有N个城市,我们将其顺序编号为1到N,1号城市为首都.这N个城 ...
- 第26天 swift reflect
1.reflect http://www.jianshu.com/p/b5c87824e33c 2.android studio 2.0项目1.5编译 Cause: com/android/build ...
- windows 下wamp环境3 安装php7
打开 http://php.net 点击download,选择Windows downloads,根据系统选择版本 注意左侧的提示: With Apache you have to use the T ...
- ZOJ 3811 Untrusted Patrol
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3811 解题报告:一个无向图上有n个点和m条边,其中有k个点上安装 ...
- iOS 开发 初级:应用内购买 In-App Purchase
http://blog.csdn.net/songrotek/article/details/8680415 现在有很多应用都使用了In-App Purchase,虽然对于很多用户来说,可能并不喜欢甚 ...
- 新技能get——斜率优化
好久没写博客了……我终于回来了…… dp总是令我很头疼的问题之一,然而我还是要学一下怎么优化它. 下面请看一道题吧: [bzoj3675][Apio2014]序列分割 试题描述 小H最近迷上了一个分割 ...
- r-cnn学习(二)
faster r-cnn 1.问题 在fast r-cnn中,proposals已经成为速度提高的瓶颈.在本文中,使用深度网络来计算proposals, 使得与检测网络的计算量相比,proposals ...
- 项目: 推送水木文章到Kindle
代码在github:https://github.com/Viyu/PushNewsmth2Mail 当年买Kindle的时候,想再Kindle上阅读水木的帖子,但Kindle的Web上网体验太差,想 ...