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 ...
随机推荐
- Scrum三头猪
猪和鸡在过去进入业务合作.他们都打算开一家餐厅,它的售价火腿和鸡蛋. 这想了一下时间猪,我用他的肉,鸡只是用它生下的蛋.万一生意失败,我自己的命就没,,没有不论什么影响. 1. Scrum两类关系人 ...
- 站长VS微商 你选择哪个?
近期,站长圈里盛行起了一阵面膜风.我刷空间和微信朋友圈的时候,常常看到一些朋友在卖面膜,不光女童鞋在卖,男屌丝站长也在卖. 不光普通人在卖.行业圈的自媒体明星大佬也在卖. 我们暂且称卖面膜的童鞋为微商 ...
- OCP-1Z0-051-标题决心-文章2称号
2. View the Exhibit to examine the description for the SALES table. Which views can have all DML ope ...
- cidaemon.exe是什么进程及怎样关闭cidaemon.exe进程
问题描写叙述: 这段时间机器总是出现一个奇怪的问题:cidaemon.exe进程占用CUP率98%以上,大大影响了电脑的正常使用.资源管理器中出现多个cidaemon.exe进程,强制结束占用cpu率 ...
- Jquery 分页插件 Jquery Pagination
Jquery 分页插件 Jquery Pagination 分页插件来说,我觉得适用就行,尽量简单然后能够根据不同的应用场景能够换肤.展现形式等. 对于初学者想写分页插件的同学,也可以看下源码,代码也 ...
- P90
#include<stdio.h> #define N 4 int fun(int a[][N]) { int i,j,max=a[0][0]; for(i=0;i<2;i++) f ...
- 写你自己 android 多通道打包工具 可以包libs和.so文件
android上传应用程序,需要区分各个信道. 通常更改配置文件中的一个通道id,假设有多个通道,手动更改并生成apk这将是非常麻烦的,及增加误差的概率. 在这个课堂上分享一个打包工具.也可在网上类似 ...
- ECLIPSE IDEA 调音 1
为自己所用IDE进行jvm优 首先进行日志输出配置 Eclipse 改动eclipse.ini IDEA 改动 idea.exe.vmoptions 添加打印日志的配置參数 -XX:+Print ...
- Android FM学习中的模块 FM启动过程
最近的研究FM模,FM是一家值我正在学习模块.什么可以从上层中可以看出. 上层是FM按钮的操作和界面显示,因此调用到FM来实现广播收听的功能. 看看Fm启动流程:例如以下图: 先进入FMRadio.j ...
- C++ - new与malloc的差别
malloc是C++语言的标准库函数:而new是C++语言中的操作符. new返回指定类型的指针,而且能够自己主动计算所需空间的大小:而malloc必需要由用户自己计算所需空间大小,并在返回后强行转换 ...