LeetCode题解——Regular Expression Matching
题目:
正则表达式的匹配,'.'能匹配任何一个字符,'*'之前必须有一个字符,两个结合起来表示之前那个字符出现0到无穷次。
解法:
一定要注意'*'必须结合前面的字符一起使用。
代码:
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+) != '*') //如果模式串的下一位不是'*',则判断当前字符
if(*s == *p || (*p == '.' && *s != '\0')) //相等,或模式串碰到万能的'.',则继续往后匹配
return isMatch(s+, p+);
else
return false;
while(*s == *p || (*p == '.' && *s != '\0')) //模式串的下一位是'*',如果当前字符相同或模式串是万能的'.'
{
if(isMatch(s, p+)) //模式串的'*'表示出现0次时的结果
return true;
++s; //s前进1,表示'*'多匹配了一个
}
return isMatch(s, p+); //'*'不匹配,跳过这个模式
}
};
LeetCode题解——Regular Expression Matching的更多相关文章
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- Leetcode 10. Regular Expression Matching(递归,dp)
10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...
- LeetCode (10): Regular Expression Matching [HARD]
https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...
- [LeetCode][Python]Regular Expression Matching
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...
- [LeetCode] 10. Regular Expression Matching 正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- [LeetCode] 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...
- 【leetcode】Regular Expression Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
- 【leetcode】Regular Expression Matching (hard) ★
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【JAVA、C++】LeetCode 010 Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
随机推荐
- lintcode:Remove Element 删除元素
题目: 删除元素 给定一个数组和一个值,在原地删除与值相同的数字,返回新数组的长度. 元素的顺序可以改变,并且对新的数组不会有影响. 样例 给出一个数组 [0,4,4,0,0,2,4,4],和值 4 ...
- 【mongoDB基础篇①】安装与常用操作语句
简述 mongoDB与redis同为noSql数据库,但是redis为kv数据库(key/value),而mongoDB为文档型数据库存储的是文档(Bson->json的二进制化).内部执行引擎 ...
- jquery通过ajax-json访问java后台传递参数,通过request.getParameter获取不到参数的说明
http://m.blog.csdn.net/blog/eyebrother/36007145 所以当后台通过request.getParameter("name");对参数值的作 ...
- .NET中操作SQLite
C#操作SQLite Database C#下SQLite操作驱动dll下载:System.Data.SQLite C#使用SQLite步骤: (1)新建一个project (2)添加SQLite操作 ...
- 如何禁用 radio ,设置为只读,不能选定
如何禁用 radio ,设置为只读,不能选定 禁用 radio ,设置为只读,不能选定: <input name="gender" type="radio" ...
- uboot---linux
01uboot是没有虚拟地址的 02内存映射是linux内核的机制,也就是从实地址到虚拟地址是linux完成的! -----
- BCB遍历所有窗体的组件
for(iFormIdx=0; iFormIdx<Screen->FormCount; iFormIdx++) { TForm *pForm = Screen->Forms[iFor ...
- Python3 学习第二弹: 字符串String
字符串表示问题 常见用法 '' 与 "" 就不提了 一些特殊用法 三引号:接收多行字符串的输入 >>>print('''Oh my God!''') Oh my ...
- UVa 10253 (组合数 递推) Series-Parallel Networks
<训练之南>上的例题难度真心不小,勉强能看懂解析,其思路实在是意想不到. 题目虽然说得千奇百怪,但最终还是要转化成我们熟悉的东西. 经过书上的神分析,最终将所求变为: 共n个叶子,每个非叶 ...
- [LA 3887] Slim Span
3887 - Slim SpanTime limit: 3.000 seconds Given an undirected weighted graph G <tex2html_verbatim ...