LeetCode(44) 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
分析
字符串匹配问题,有两种解决思路:
- 采用递归实现,但是性能不好,TLE;
- 贪心解决,参考网址
AC代码
class Solution {
public:
//字符串模式匹配问题,s为匹配串,p为模式串
bool isMatch(string s, string p) {
return isMatch1(s.c_str(), p.c_str());
}
/*方法一:采用贪心的性质 AC*/
bool isMatch1(const char *s, const char *p) {
//? match one
//* match 0,1,2,3..
// aaaabc *c true
const char* star = nullptr;
const char* rs = nullptr;
while (*s) {
if (*s == *p || *p == '?') { //match
s++; p++;
continue;
}
if (*p == '*') {
star = p; // record star
p++; //match from next p
rs = s; // record the position of s , star match 0
continue;
}
if (star != nullptr) { //if have star in front then backtrace
p = star + 1; //reset the position of p
s = rs + 1;
rs++; //star match 1,2,3,4,5....
continue;
}
return false; //if not match return false
}
while (*p == '*') p++; //skip continue star
return *p == '\0'; // successful match
}
/*方法二:利用字符串指针递归解决 性能不好 TLE*/
bool isMatch2(const char *s, const char *p)
{
if (p == NULL || s == NULL)
return false;
else if (*p == '\0')
return (*s == '\0');
else
{
if (*p == '*')
{
while (*p == '*')
++p;
/*处理'*'可以匹配任意长度任意格式的字符串*/
while (*s != '\0')
{
if (isMatch2(s, p))
return true;
++s;
}//while
return isMatch2(s, p);
}
else if((*s!='\0' && *p == '?') || (*s == *p)){
return isMatch2(++s, ++p);
}//else
return false;
}//else
}
};
LeetCode(44) Wildcard Matching的更多相关文章
- [Leetcode][Python]44:Wildcard Matching
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 44:Wildcard Matchinghttps://oj.leetcode ...
- LeetCode(44): 通配符匹配
Hard! 题目描述: 给定一个字符串 (s) 和一个字符模式 (p) ,实现一个支持 '?' 和 '*' 的通配符匹配. '?' 可以匹配任何单个字符. '*' 可以匹配任意字符串(包括空字符串). ...
- Leetcode(8)字符串转换整数
Leetcode(8)字符串转换整数 [题目表述]: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(44)-工作流设计-设计表单
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(44)-工作流设计-设计表单 系列目录 设计表单是比较复杂的一步,完成一个表单的设计其实很漫长,主要分为四 ...
- Windows Phone开发(44):推送通知第二集——磁贴通知
原文:Windows Phone开发(44):推送通知第二集--磁贴通知 前面我们说了第一个类型--Toast通知,这玩意儿不知大家是不是觉得很新鲜,以前玩.NET编程应该没接触过吧? 其实这东西绝对 ...
- (转)每天一个linux命令(44):top命令
背景:在面试时候面试官问到关于linux服务器下内存优化的问题.自己之前可能接触过也没有深入总结过. top命令 每天一个linux命令(44):top命令
- Qt 学习之路 2(44):QFileSystemModel
Home / Qt 学习之路 2 / Qt 学习之路 2(44):QFileSystemModel Qt 学习之路 2(44):QFileSystemModel 豆子 2013年2月21日 Qt ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
随机推荐
- Activemq和Rabbitmq端口冲突
端口5672被占用了!使用cmd命令行查到被erl.exe占用了,想到这不是RabbitMQ的服务嘛 netstat -aon | findstr "5672"tasklist | ...
- JVM类加载之创建对象的过程
JVM对象的创建 new一个对象到底发生了什么? 1.VM遇到new关键字:首先检查是否能在常量池中定位到一个类的符号引用,并检查这个符号引用代表的类是否被加载,验证,准备,解析,初始化过,如果没有, ...
- It is not the destination so much as the journey, they say.
It is not the destination so much as the journey, they say. 人家说目的地不重要,重要的是旅行的过程.<加勒比海盗>
- 文件系统结构-《循序渐进linux》
1.目录结构 很多linux的发行版都遵循FSSTND标准,这一标准仅包含系统最基本的文件. /dev 设备文件 /bin 可执行的二进制文件 /opt /root 超级用户的主目录 /home 每个 ...
- FAST FW150R软件版本升级解决一些网页无法加载问题
家里用的移动宽带,通过无线路由器无线上网.上taobao.天猫都很快,但是一上京东.苏宁易购界面加载很慢,界面无法显示,怀疑是无线路由问题,然后直接通过网线相连接,发现问题消失,决定对无线路由软件版本 ...
- UIButton 图片文字位置
在实际开发过程中经常在按钮上添加文字和图片,位置和图片的位置根据需求放置也是不一样的.下面实现了各种显示方式,如下图: UIButton+LSAdditions.h // // UIButton+LS ...
- 从wireshark数据中分析rtmp协议,并提取出H264视频流
我写的小工具 rtmp_parse.exe 使用用法如先介绍下: -sps [文件路径] 解析 sps 数据 文件当中的内容就是纯方本的hexstring: 如 42 E0 33 8D 68 05 ...
- python发送邮件 示例
示例1 import smtplib from email.mime.text import MIMEText from email.header import Header def sedmail( ...
- less的使用总结
简单执行less 一.使用npm全局安装less: npm install -g less 二.创建less文件 三.执行命令将less文件转换成css文件 lessc less/style.less ...
- NVM for Windows下载与安装
下载NVM for Windows https://github.com/coreybutler/nvm-windows/releases nvm-noinstall.zip: 这个是绿色免安装版本, ...