leetcode第十题--Regular Expression Matching
Problem: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
就是正则表达式,可以百度一下先学习什么事正则表达式。*号是表示前面一个字符出现零次或者多次。例如 zo*能和z匹配是因为此处*表示前面的字符o出现零次,所以和z匹配,同理,zo*还可以和zo或者zoo,zoooo,zooooooooo等等匹配。点‘.’是通配符,匹配任意一个单字符。那么点星‘.*’就可以匹配任意多个字符,因为*表示前面的任意多个重复。所以就是任意多个‘.’的重复,而‘.’又是任意匹配,所以就是任意组合都可以。第一个点匹配a第二个点并不是只能匹配a,还是可以匹配任意的字符。所以‘.*’确实是万能的。应该没有理解错误吧。 然后这题,考虑了很久,都没有想到好的思路,不得不用递归。
class Solution {
public:
bool isMatch(const char *s, const char *p)
{
if(s == NULL || p == NULL)
return false;
if (*p == '\0')
return *s == '\0';
if (*(p + ) == '*')
{
while(*s == *p || *p == '.' && *s != '\0')
{
if (isMatch(s, p + ))
return true;
s++;
}
return isMatch(s, p + );
}
else if (*s == *p || *p == '.'&&*s!='\0')
return isMatch(s + , p + );
return false;
}
};
提交尝试好多次把 if (*p == '\0') return *s == '\0'; 忽略。因为是递归的,所以不能没有。这里的'.'不能对应‘\0’
也可以参考这位同学的。
leetcode第十题--Regular Expression Matching的更多相关文章
- LeetCode(10) Regular Expression Matching
题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single charac ...
- LeetCode(10)Regular Expression Matching
题目如下: Python代码: # -*- coding:utf-8 -*- def ismatch(s,p): #先将dp[s+1][p+1]二维数组全置为False dp = [[False] * ...
- 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman
[Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...
- LeetCode第[10]题(Java):Regular Expression Matching
题目:匹配正则表达式 题目难度:hard 题目内容:Implement regular expression matching with support for '.' and '*'. '.' Ma ...
- 【leetcode刷题笔记】Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- [LeetCode] Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- LeetCode (10): Regular Expression Matching [HARD]
https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...
- 【一天一道LeetCode】#10. Regular Expression Matching
一天一道LeetCode系列 (一)题目 Implement regular expression matching with support for '.' and '*'. '.' Matches ...
- [leetcode]Regular Expression Matching @ Python
原题地址:https://oj.leetcode.com/problems/regular-expression-matching/ 题意: Implement regular expression ...
随机推荐
- Linux服务器杀马(转)
开篇前言 Linux服务器一直给我们的印象是安全.稳定.可靠,性能卓越.由于一来Linux本身的安全机制,Linux上的病毒.木马较少,二则由于宣称Linux是最安全的操作系统,导致很多人对Linux ...
- 安卓模拟器错误: Could not open
在进行android模拟器測试的时候,出现下面错误,进度条满了之后就没反应了.图例如以下: 引起这个问题的可能原因有非常多: 原因一:由于我们採用的是绝对路径定位.也就是说在环境变量里面把路径写死了, ...
- (大数据工程师学习路径)第四步 SQL基础课程----创建数据库并插入数据
一.练习内容 1.新建数据库 首先,我们创建一个数据库,给它一个名字,比如“mysql_shiyan”,以后的几次实验也是对mysql_shiyan这个数据库进行操作. 语句格式为“CREATE DA ...
- SQL Server的备份
原文:SQL Server的备份 0.参考文献 1.恢复模式 SQL Server 备份和还原操作发生在数据库的恢复模式的上下文中. 恢复模式旨在控制事务日志维护. “恢复模式”是一种数据库属性,它控 ...
- 《Java并发编程实战》第十四章 构建自己的同步工具定义 札记
一.状态依赖性的管理 有界缓存实现的基类 @ ThreadSafe public abstract class BaseBoundedBuffer<E> { @GuardeBy( &quo ...
- 创建自定义的Middleware中间件
创建自定义的Middleware中间件 阅读目录 何为Middleware中间件 使用Inline方式注册Middleware 使用Inline+ AppFunc方式注册Middleware 定义原生 ...
- RabbitHub开源
RabbitHub开源情况及计划 之前写过一篇”.NET 平台下的插件化开发内核(Rabbit Kernel)”,已经过去三个月了,期间RabbitHub并不是没有了发展更不是放弃了发展,在Rab ...
- HttpClient模拟客户端请求实例
HttpClient Get请求: /// <summary> /// Get请求模拟 /// </summary> /// < ...
- WebApi路由及版本控制
public class WebApiControllerSelector : IHttpControllerSelector { private const string NamespaceKey ...
- elasticsearch的rest搜索--- 查询
目录: 一.针对这次装B 的解释 二.下载,安装插件elasticsearch-1.7.0 三.索引的mapping 四. 查询 五.对于相关度的大牛的文档 四. 查询 1. 查询的官网的文档 ...