题目

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

分析

字符串匹配问题,有两种解决思路:

  1. 采用递归实现,但是性能不好,TLE;
  2. 贪心解决,参考网址

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
}
};

GitHub测试程序源码

LeetCode(44) Wildcard Matching的更多相关文章

  1. [Leetcode][Python]44:Wildcard Matching

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 44:Wildcard Matchinghttps://oj.leetcode ...

  2. LeetCode(44): 通配符匹配

    Hard! 题目描述: 给定一个字符串 (s) 和一个字符模式 (p) ,实现一个支持 '?' 和 '*' 的通配符匹配. '?' 可以匹配任何单个字符. '*' 可以匹配任意字符串(包括空字符串). ...

  3. Leetcode(8)字符串转换整数

    Leetcode(8)字符串转换整数 [题目表述]: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我 ...

  4. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(44)-工作流设计-设计表单

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(44)-工作流设计-设计表单 系列目录 设计表单是比较复杂的一步,完成一个表单的设计其实很漫长,主要分为四 ...

  5. Windows Phone开发(44):推送通知第二集——磁贴通知

    原文:Windows Phone开发(44):推送通知第二集--磁贴通知 前面我们说了第一个类型--Toast通知,这玩意儿不知大家是不是觉得很新鲜,以前玩.NET编程应该没接触过吧? 其实这东西绝对 ...

  6. (转)每天一个linux命令(44):top命令

    背景:在面试时候面试官问到关于linux服务器下内存优化的问题.自己之前可能接触过也没有深入总结过. top命令 每天一个linux命令(44):top命令

  7. Qt 学习之路 2(44):QFileSystemModel

    Home / Qt 学习之路 2 / Qt 学习之路 2(44):QFileSystemModel Qt 学习之路 2(44):QFileSystemModel  豆子  2013年2月21日  Qt ...

  8. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  9. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

随机推荐

  1. Struts2 拦截器(Interceptor )原理和配置

    http://blog.csdn.net/kingmax54212008/article/details/51777851

  2. DNS学习

    DNS (Domain Name System 的缩写)域名系统,万维网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串.通过域名 ...

  3. cf519D. A and B and Interesting Substrings(前缀和)

    题意 给出$26$个字母对应的权值和一个字符串 问满足以下条件的子串有多少 首尾字母相同 中间字母权值相加为0 Sol 我们要找到区间满足$sum[i] - sum[j] = 0$ $sum[i] = ...

  4. java-web项目:用servlet监听器实现显示在线人数

    1.创建一个监听器 package com.listener; import javax.servlet.ServletContext; import javax.servlet.http.HttpS ...

  5. web常见几种处理图标方法

    方法一: 用background制作小图标 像这样,拿到设计稿后把所有的图标放在一张图片上,利用background-position.width.height来控制图标的位置及大小. 代码: .ic ...

  6. js之深度克隆、简易克隆

    一.js中的对象 谈到对象的克隆,必定要说一下对象的概念. js中的数据类型分为两大类:原始类型和对象类型. (1)原始类型包括:数值.字符串.布尔值.null.undefined(后两个是特殊的原始 ...

  7. C++ error:Debug Assertion Failed.Expression:_BLOCK_TYPE_IS_VALID(phead->nBlock)

    Debug Assertion Failed.Expression:_BLOCK_TYPE_IS_VALID(phead->nBlockUse) 关于上面这个错误,我在上一篇文章中的程序遇到过了 ...

  8. Java 基础案例

    1.变量及基本数据类型 案例1:变量声明及赋值 //1.变量的声明 int a; //声明一个整型的变量a int b,c,d; //声明三个整型变量b,c,d //2.变量的初始化 int a = ...

  9. UVA 11988 Broken Keyboard (链表)

    简单题,题目要求显然是很多次插入,所以是链表. 插入两个语句,nxt[i] = nxt[u] 表示 i结点指向u的后继, nxt[u] = i表示把u的后继设成i. 设置一个头结点,指向一个不存在的结 ...

  10. UI的组织形式

    UI的组织形式是树状结构: 根据层次的不同分为叶子节点和干节点. 叶子节点负责简单的信息展示. 复杂的主干复杂叶子节点的组织和整体展示. http://www.cnblogs.com/feng9exe ...