题目

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代码

  1. class Solution {
  2. public:
  3. //字符串模式匹配问题,s为匹配串,p为模式串
  4. bool isMatch(string s, string p) {
  5. return isMatch1(s.c_str(), p.c_str());
  6. }
  7. /*方法一:采用贪心的性质 AC*/
  8. bool isMatch1(const char *s, const char *p) {
  9. //? match one
  10. //* match 0,1,2,3..
  11. // aaaabc *c true
  12. const char* star = nullptr;
  13. const char* rs = nullptr;
  14. while (*s) {
  15. if (*s == *p || *p == '?') { //match
  16. s++; p++;
  17. continue;
  18. }
  19. if (*p == '*') {
  20. star = p; // record star
  21. p++; //match from next p
  22. rs = s; // record the position of s , star match 0
  23. continue;
  24. }
  25. if (star != nullptr) { //if have star in front then backtrace
  26. p = star + 1; //reset the position of p
  27. s = rs + 1;
  28. rs++; //star match 1,2,3,4,5....
  29. continue;
  30. }
  31. return false; //if not match return false
  32. }
  33. while (*p == '*') p++; //skip continue star
  34. return *p == '\0'; // successful match
  35. }
  36. /*方法二:利用字符串指针递归解决 性能不好 TLE*/
  37. bool isMatch2(const char *s, const char *p)
  38. {
  39. if (p == NULL || s == NULL)
  40. return false;
  41. else if (*p == '\0')
  42. return (*s == '\0');
  43. else
  44. {
  45. if (*p == '*')
  46. {
  47. while (*p == '*')
  48. ++p;
  49. /*处理'*'可以匹配任意长度任意格式的字符串*/
  50. while (*s != '\0')
  51. {
  52. if (isMatch2(s, p))
  53. return true;
  54. ++s;
  55. }//while
  56. return isMatch2(s, p);
  57. }
  58. else if((*s!='\0' && *p == '?') || (*s == *p)){
  59. return isMatch2(++s, ++p);
  60. }//else
  61. return false;
  62. }//else
  63. }
  64. };

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. (转)通过MySQL复制线程SQL_Thread加快增量恢复binlog

    数据回档常常是使用全量备份+binlog增量实现的.而数据量很大的情况下,增量恢复binlog一直是一个苦恼的问题,因为恢复binlog速度十分慢,并且容易出错. 恢复binlog文件一般有两种方法: ...

  2. C#中的XML文档注释-推荐的文档注释标记

    文档注释是为了方便自己和他人更好地理解代码所实现的功能.下面记录了一些常用的文档注释标记: <C> 用法: <c>text</c> 将说明中的文本标记为代码.例如: ...

  3. asp.net MVC 4.0 View回顾——布局页与分部页

    asp.net MVC 4.0中总结 视图里加载部分视图几种方法 @RenderPage() 但它不能使用 原来视图的 Model 和 ViewData ,只能通过参数来传递. @RenderPage ...

  4. sqlserver跟据当天年月日日期查询数据库当天数据

    select * from Client where  CONVERT(varchar(100), Cli_Datetime, 23) ='2017-11-06' 在查询之前要对表中datetime类 ...

  5. java读取文件封装的一个类(有部分代码借鉴别人的)

    package modbus.rtu.calc; import java.io.BufferedReader; import java.io.FileInputStream; import java. ...

  6. layui内置loading等待加载

    点击功能按钮之后 var loading = layer.load(0, { shade: false, time: 2*1000 }); 参数: icon:0,1,2 loading风格 shade ...

  7. 文件系统结构-《循序渐进linux》

    1.目录结构 很多linux的发行版都遵循FSSTND标准,这一标准仅包含系统最基本的文件. /dev 设备文件 /bin 可执行的二进制文件 /opt /root 超级用户的主目录 /home 每个 ...

  8. Python3+Selenium3+webdriver学习笔记10(元素属性、页面源码)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记10(元素属性.页面源码)'''from selenium i ...

  9. Android(java)学习笔记124:利用Service在后台播放背景音乐

    1. 在android应用程序里,有一种没有UI的类(android.app.Service)——Service.简单来说,Service是一个 background process(背景程序),通过 ...

  10. [学习总结] python语言学习总结 (二)

    1.python中的拆包 之前就只写了*可以是未知数量的参数,**可以传入未知数量命名参数.这次详细记下拆包. def f1(a, *l): print(a) # 不拆包 print(l) # 拆包 ...