题目描述:Regular Expression Matching

Implement regular expression matching with support for '.' and '*'

'.' Matches any single character.
'*' Matches zero or more of the preceding element. 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", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true

代码如下:

class Solution {
public:
bool isMatch(const char *s, const char *p) { if(*p == '\0') return *s == '\0'; //如果下一个字符不是*,那么就需要匹配当前的字符!
if(*(p + 1) != '*'){ //下一个字符相等,或者有一个是'.',则匹配成功
if(*p == *s || (*p == '.' && *s != '\0'))
return isMatch(s + 1, p + 1);
else
return false;
} //下一个字符是*,则当前字符可以为0个或多个!
else{
while(*p == *s || (*p == '.' && *s != '\0')){
if(isMatch(s, p + 2))
return true;
s++;
}
return isMatch(s, p + 2);
} }
};

LeetCode 010 Regular Expression Matching的更多相关文章

  1. 【JAVA、C++】LeetCode 010 Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  2. leetcode 10 Regular Expression Matching(简单正则表达式匹配)

    最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...

  3. 【LeetCode】010. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  4. LeetCode (10): Regular Expression Matching [HARD]

    https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...

  5. [LeetCode][Python]Regular Expression Matching

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...

  6. 010 Regular Expression Matching 正则表达式匹配

    Implement regular expression matching with support for '.' and '*'.'.' Matches any single character. ...

  7. [LeetCode] 10. Regular Expression Matching 正则表达式匹配

    Given an input string (s) and a pattern (p), implement regular expression matching with support for  ...

  8. Leetcode 10. Regular Expression Matching(递归,dp)

    10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...

  9. [LeetCode] 10. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...

随机推荐

  1. Linux使用tmux

    Tmux功能: 提供了强劲的.易于使用的命令行界面.可横向和纵向分割窗口.窗格可以自由移动和调整大小,或直接利用四个预设布局之一.支持 UTF-8 编码及 256 色终端.可在多个缓冲区进行复制和粘贴 ...

  2. Docker(2)- Centos 7.x 下安装 Docker

    如果你还想从头学起 Docker,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1870863.html 前言 虚拟机安装 centos ...

  3. How to get last SysExcelWorksheet object row or column[X++]

    findLastColumn int findLastColumn(SysExcelWorksheet _sysExcelWorksheet, boolean _data = true) { #Exc ...

  4. 管理机--Jumpserver由docker搭建

    一.环境准备 使用Centos7.0及以上版本,(网要好哦) 二.安装docker 1,下载,安装,启动 docker yum -y install docker         #安装docker ...

  5. Git Push大文件报错后如何撤回

    昨晚在提交一个项目代码时,不小心把数据库备份文件也一起Commit了:到最后Push的时候报错了.最后弄了半天解决了,在此记录下. 如下图,文件有108M. 项目放在第三方托管平台上,根据提示查看了原 ...

  6. ASP.NET MVC过滤器粗略总结

    mvc的过滤器总共分为:授权认证过滤器.行为过滤器.异常过滤器.自定义行为过滤器 授权认证过滤器: 异常过滤器: 行为过滤器的调用顺序: 几个过滤器在Controler的使用

  7. Spring源码解析之BeanFactoryPostProcessor(一)

    BeanFactoryPostProcessor 在前面几个章节,笔者有介绍过BeanFactoryPostProcessor,在spring在解析BeanDefinition之后,正式初始化bean ...

  8. python_socket_tcp_文件传输

    server.py import json import struct import socket # 接收 sk = socket.socket() # sk.bind(('127.0.0.1',9 ...

  9. 企业网络拓扑VRRP主备功能实例(一)

    组网图形  VRRP主备备份简介 通常,同一网段内的所有主机上都存在一条相同的.以网关为下一跳的缺省路由.主机发往其他网段的报文将通过缺省路由发往网关,再由网关进行转发,从而实现主机与外部网络的通信. ...

  10. stm32串口的配置方案

    最近老板要我去做控制方面的内容,所以买了一块正点原子的开发板,现在是研究了一下usart.c,函数的代码如下: void USART1_IRQHandler(void) { u8 Res; #ifde ...