题目

正则表达式的匹配,'.'能匹配任何一个字符,'*'之前必须有一个字符,两个结合起来表示之前那个字符出现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. 2014--9=17 软工二班 MyEclipse blue==修改浏览器语言

  2. ubuntu下android源码下载

    步骤一: 首先保证你的ubuntu系统电脑可以顺利游览google,我们是将etc下 hosts替换掉,推荐hosts: http://laod.cn/hosts/2015-google...host ...

  3. sqlsevrer中output的用法

    近日,看到代码中有output写法,不知其意,经过一番查找,终于找到了原因,它的作用是将修改影响的结果给输出出来. 比如update语句, 除了修改数据以外, 对于发生更新的列, update语句还可 ...

  4. hive 学习笔记——表的入门操作和命令

    1.受控表(managed table)包括内部表.分区表.桶表: 1.1.分区表 创建分区表: create table banji(id INT,name STRING) partitioned ...

  5. /usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.15' not found错误的解决

    升级cmake时,提示"Error when bootstrapping CMake:Problem while running initial CMake",第二次运行./boo ...

  6. oracle tns in linux

    [oracle@redhat4 admin]$ cd $ORACLE_HOME/network/admin[oracle@redhat4 admin]$ cat tnsnames.ora# tnsna ...

  7. Codeforces Round #243 (Div. 2) C. Sereja and Swaps(优先队列 暴力)

    题目 题意:求任意连续序列的最大值,这个连续序列可以和其他的 值交换k次,求最大值 思路:暴力枚举所有的连续序列.没做对是因为 首先没有认真读题,没看清交换,然后,以为是dp或者贪心 用了一下贪心,各 ...

  8. bzoj3242

    如果是树,那么一定选择树的直径的中点 套了个环?裸的想法显然是断开环,然后求所有树的直径的最小值 环套树dp的一般思路,先把环放到根,把环上点下面的子树dp出来,然后再处理环上的情况 设f[i]表示以 ...

  9. C#中结构体的声明

    定义:       结构是用户自定义的值类型 代码样式:struct Pair{    public int X, Y; //公有变量名单词的首字母大写(PascalCase规则)}struct Pa ...

  10. Flexigrid自定义显示数据列

    近期在搞ExtJs,发现ExJs的Grid相当的强大,后来又搞Jquery时,就对原来的表格不怎么满意了,于是,花了点时间,从网上找了个Grid插件,这个插件功能是比较强大,什么行排序.筛选.分页都有 ...