[leetcode]Wildcard Matching @ Python
原题地址:https://oj.leetcode.com/problems/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
解题思路:又是一个极其巧妙的解法。
Analysis:
For each element in s
If *s==*p or *p == ? which means this is a match, then goes to next element s++ p++.
If p=='*', this is also a match, but one or many chars may be available, so let us save this *'s position and the matched s position.
If not match, then we check if there is a * previously showed up,
if there is no *, return false;
if there is an *, we set current p to the next element of *, and set current s to the next saved s position.
e.g.
abed
?b*d**
a=?, go on, b=b, go on,
e=*, save * position star=3, save s position ss = 3, p++
e!=d, check if there was a *, yes, ss++, s=ss; p=star+1
d=d, go on, meet the end.
check the rest element in p, if all are *, true, else false;
Note that in char array, the last is NOT NULL, to check the end, use "*p" or "*p=='\0'".
代码:
class Solution:
# @param s, an input string
# @param p, a pattern string
# @return a boolean
# @good solution! use 'aaaabaaaab' vs 'a*b*b' as an example
def isMatch(self, s, p):
pPointer=sPointer=ss=0; star=-1
while sPointer<len(s):
if pPointer<len(p) and (s[sPointer]==p[pPointer] or p[pPointer]=='?'):
sPointer+=1; pPointer+=1
continue
if pPointer<len(p) and p[pPointer]=='*':
star=pPointer; pPointer+=1; ss=sPointer;
continue
if star!=-1:
pPointer=star+1; ss+=1; sPointer=ss
continue
return False
while pPointer<len(p) and p[pPointer]=='*':
pPointer+=1
if pPointer==len(p): return True
return False
[leetcode]Wildcard Matching @ Python的更多相关文章
- 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 字符串匹配,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 ...
- [Leetcode][Python]44:Wildcard Matching
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 44:Wildcard Matchinghttps://oj.leetcode ...
随机推荐
- NLP文本相似度(TF-IDF)
本篇博文是数据挖掘部分的首篇,思路主要是先聊聊相似度的理论部分,下一篇是代码实战. 我们在比较事物时,往往会用到“不同”,“一样”,“相似”等词语,这些词语背后都涉及到一个动作——双方的比 ...
- 数据转换bug花了半天时间 Java.math.BigDecimal cannot be cast to java.lang.String
从数据库取出一个 Count函数 统计的值 在代码中要转成Integer类型的时候 Integer.parseInt((String)map.get("ID_")) 报了一下错误: ...
- [NOIp2014提高组]解方程
思路: 系数的范围有$10^{10000}$,但是用高精度做显然不现实,因此可以考虑一个类似于“哈希”的做法, 对方程两边同时取模,如果取的模数足够多,正确率就很高了. 中间对多项式的计算可以使用$O ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) C. Table Tennis Game 2 水题
C. Table Tennis Game 2 题目连接: http://codeforces.com/contest/765/problem/C Description Misha and Vanya ...
- 【转】NHibernate对象以及状态说明
对象 ISessionFactory (NHibernate.ISessionFactory) 针对单个数据库映射关系经过编译后的内存镜像,是线程安全的(不可变). 它是生成ISession的工厂,本 ...
- 【面试虐菜】—— JAVA面试题(1)
今天参加笔试,里面有设计模式,和一些基础题! 印象最深的是:什么不是Object的函数,我蒙的finalize,哎,无知! 还问了,接口与抽象类的不同,还有多线程的实现方式!下面基本都有. 另外还问了 ...
- FireDAC 下的 Sqlite [1] - 前言
很长时间没静下心来写博客了, 现在回来, 是 Delphi 不断地进步让我感动.振奋. Delphi XE5 并入了 FireDAC, 第一印象非常好, 恐怕 dbExpress 等等都要靠边站了. ...
- android 的几个黄色警告解决办法(转)
转自:http://my.eoe.cn/864234/archive/5162.html 1:Handler 1 2 3 4 5 6 7 8 // This Handler class should ...
- Jack--10天学会IOS大纲;注意将图片放大观看!
第一天:磨刀霍霍期 耐得住性子好好熟悉和认识开发环境 ---------Jack/版权全部 认识开发环境 操作系统认识 Mac系统是苹果机专用系统.是基 ...
- Project 03- STM32F4xx PID controller
Project 03- STM32F4xx PID controller CMSIS files from ARM provides ARM Math functions. There are als ...