leetcode-wildcard matching-ZZ
http://yucoding.blogspot.com/2013/02/leetcode-question-123-wildcard-matching.html
几个例子:
(1)
acbdeabd
a*c*d
(2)
acbdeabdkadfa
a*c*dfa
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'".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
class Solution { public : bool isMatch( const char *s, const char *p) { // Start typing your C/C++ solution below // DO NOT write int main() function const char * star=NULL; const char * ss=s; while (*s){ if ((*p== '?' )||(*p==*s)){s++;p++; continue ;} if (*p== '*' ){star=p++; ss=s; continue ;} if (star){ p = star+1; s=++ss; continue ;} return false ; } while (*p== '*' ){p++;} return !*p; } }; |
leetcode-wildcard matching-ZZ的更多相关文章
- 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 ...
- [Leetcode][Python]44:Wildcard Matching
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 44:Wildcard Matchinghttps://oj.leetcode ...
随机推荐
- 赋值运算与深浅copy
1.复制运算 l1 = [1,2,3,['a','b']] l2 = l1 l1[0] = 111 print(l1) # [111, 2, 3, ['a', 'b']] print(l2) # [1 ...
- 基于python实现Oracle数据库连接查询操作
使用python语言连接Oracle数据库配置 #coding:utf-8 import cx_Oracle as oracle db=oracle.connect('root/123456@192. ...
- Java - 自定义异常(尚学堂第六章异常机制作业判断三角形)
写一个方法void isTriangle(int a,int b,int c),判断三个参数是否能构成一个三角形, 如果不能则抛出异常IllegalArgumentException,显示异常信息 “ ...
- display:inline-block会出现的问题
用一个父元素包裹三个子元素,将父元素的white-space设置为nowrap;这样子元素就会排在父元素中而不会换行了,通过这种方式,我们也就可以在移动端使用包裹元素的margin值实现类似的单页应用 ...
- condition实现原理
condition是对线程进行控制管理的接口,具体实现是AQS的一个内部类ConditionObject,主要功能是控制线程的启/停(这么说并不严格,还要有锁的竞争排队). condition主要方法 ...
- unity状态机实现
刚看了浅墨大神的文章让我对状态机有了进一步的理解 具体实现见装载的状态机文章 首先得有个总状态HeroineBaseState接口,其里面的方法主要是与行为相关的方法,让继承此接口的类来实现的 具体的 ...
- Python基础(3) - 数据类型:1数字类型
Python数据类型 数据类型 是否容器 是否可变 存储方式 数字 否 否 直接 字符串 否 否 直接 列表 是 是 顺序 元组 是 否 顺序 字典 是 是 映射 数字类型 整 型:1,234,0, ...
- 【c++】动态绑定
C++的函数调用默认不使用动态绑定.要触发动态绑定,必须满足两个条件: 只有指定为虚函数的成员函数才能进行动态绑定 必须通过基类类型的引用或指针进行函数调用 因为每个派生类对象中都拥有基类部分,所以可 ...
- alpine 上部署netcore 项目
1 Alpine部署 注:以下教程是以Alpine v3.7.0系统部署:其他Linux系统部署也基本相同 1.1 .NET Core环境包下载 .net core下载地址:https://dotne ...
- 解决github无法访问的问题
gitbub是外网,经常会遇到访问不了的问题,并且有时能访问也网速好慢. 解决这个问题的方法是 更改hosts文件,地址: C:\Windows\System32\Drivers\etc 我在hos ...