正则表达式的匹配,还是挺难的。可根据下一个字符是不是*分为两种情况处理,需要考虑多种情况。

bool isMatch(const char *s, const char *p)
{
if (*p == '\0')return *s == '\0'; //如果下一个不是*(*可表示前一个字符的数量)
//要么当前字符匹配,要么是.,不可跳过
if (*(p + ) != '*')
{
if (*s == *p || (*p == '.' && *s != '\0'))
return isMatch(s + , p + );
else
return false;
}
else
{
//如果是*,则当前字符匹配|| 有一个为.
//因为后面是*,即使不完全匹配也没关系,跳过即可
while (*p == *s || (*p == '.' && *s != '\0'))
{
if (isMatch(s, p + ))
return true;
s++;
}
return isMatch(s, p + );
}
}

Leetcode 之Regular Expression Matching(31)的更多相关文章

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

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

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

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

  3. [LeetCode][Python]Regular Expression Matching

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

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

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

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

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

  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 ...

  10. Java [leetcode 10] Regular Expression Matching

    问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...

随机推荐

  1. POJ3977:Subset——题解(三分+折半搜索)

    http://poj.org/problem?id=3977 题目大意:有一堆数,取出一些数,记他们和的绝对值为w,取的个数为n,求在w最小的情况下,n最小,并输出w,n. ————————————— ...

  2. POJ2142:The Balance——题解

    http://poj.org/problem?id=2142 题目大意:有一天平和两种数量无限的砝码(重为a和b),天平左右都可以放砝码,称质量为c的物品,要求:放置的砝码数量尽量少:当砝码数量相同时 ...

  3. BZOJ3435 & 洛谷3920 & UOJ55:[WC2014]紫荆花之恋

    https://www.lydsy.com/JudgeOnline/problem.php?id=3435 https://www.luogu.org/problemnew/show/P3920 ht ...

  4. 洛谷P3759 [TJOI2017]不勤劳的图书管理员 【树状数组套主席树】

    题目链接 洛谷P3759 题解 树状数组套主席树板题 #include<algorithm> #include<iostream> #include<cstring> ...

  5. BZOJ1202 [HNOI2005]狡猾的商人 【并查集】

    1202: [HNOI2005]狡猾的商人 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 4180  Solved: 2015 [Submit][S ...

  6. AOJ 7.Redraiment猜想

    Redraiment猜想 Description redraiment在家极度无聊,于是找了张纸开始统计素数的个数. 设函数f(n)返回从1->n之间素数的个数. redraiment发现: f ...

  7. nodejs路径处理方法和绝对路径

    1. 路径处理方法 __dirname 表示当前文件所在的目录的绝对路径__filename 表示当前文件的绝对路径module.filename ==== __filename 等价process. ...

  8. 【状压DP】【P3959】【NOIP2017D2T2】宝藏

    Description 参与考古挖掘的小明得到了一份藏宝图,藏宝图上标出了 $n$ 个深埋在地下的宝藏屋, 也给出了这 $n$ 个宝藏屋之间可供开发的$ m$ 条道路和它们的长度. 小明决心亲自前往挖 ...

  9. JS判断当前DOM树是否加载完毕

    /** * @function Monitor whether the document tree is loaded. * @param fn */function domReady(fn) { i ...

  10. luncence

    问题的提出: 我们在访问淘宝,京东这些商城系统的时候,我们可以随意的在文本框输入关键字就可以获取到所想要的信息或者相关的信息,那么我们到底是如何实现这个功能的呢,为什么可以随意的输入就可以显示相关的信 ...