题目

正则表达式的匹配,'.'能匹配任何一个字符,'*'之前必须有一个字符,两个结合起来表示之前那个字符出现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的更多相关文章

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

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

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

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

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

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

  4. [LeetCode][Python]Regular Expression Matching

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

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

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

  6. [LeetCode] 10. Regular Expression Matching

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

  7. 【leetcode】Regular Expression Matching

    Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...

  8. 【leetcode】Regular Expression Matching (hard) ★

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

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

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

随机推荐

  1. jsp片段

    转载自:http://blog.csdn.net/lovejavaydj/article/details/7293145 使用jspf 在开发中写jsp页面时,通常都要通过如下方式在jsp文件头部引入 ...

  2. linux服务器下发送邮件

    系统管理人员经常会遇到对于设备或者任务的预警与通知,通常情况有发送短信.邮件等方式.发送短信一般来说需要有短信猫(硬件)或者调用libfetion给飞信用户发送.本文介绍几种简单的发送邮件的方式. 本 ...

  3. java:访问权限-protected实例

    在不同包,子类继承后可以使用父类的protect权限的属性或方法 父类: package com.tinyphp; public class Father { protected String nam ...

  4. 向ArcMap添加未出现的工具 如planarize lines

        打开某工具的customize 找到你要添加的工具 将其拖到你要放置的工具条即可

  5. 如何禁用 radio ,设置为只读,不能选定

    如何禁用 radio ,设置为只读,不能选定 禁用 radio ,设置为只读,不能选定: <input name="gender" type="radio" ...

  6. 【Spring】Redis的两个典型应用场景--good

    原创 BOOT Redis简介 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结构,例如hashes, lists, sets等,同时支持数据持久化. ...

  7. QTP不能打开或者新建FunctionLibrary的解决方法

    今天打开QTP,然后打开function library的时候,qtp窗口右下角一直都是open...状态,怀疑是qtp与其他的软件冲突了. 解决方法: 直接执行QTP安装程序,然后选择修复QTP,问 ...

  8. C#sqlbulkcopy的优化

    最近在进行项目的优化.现在部分数据的拷贝时间过长.需要进行上线前的优化,尝试,批次的数量和拷贝次数的之间的合理数值关系. 最近项目中使用到了SqlBulkCopy实现批量复制,在这里,我把部分代码筛选 ...

  9. Android相对布局(RelativeLayout)

    Android相对布局(RelativeLayout) 备注:这里的视图和元素是等同的概念. RelativeLayout是一个允许子视图相对于其他兄弟视图或是父视图显示的视图组(通过ID指定).每个 ...

  10. ssl选购

    上机实践,参考了: http://www.lovelucy.info/nginx-ssl-certificate-https-website.html http://nginx.org/cn/docs ...