题目

正则表达式的匹配,'.'能匹配任何一个字符,'*'之前必须有一个字符,两个结合起来表示之前那个字符出现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. Tomcat部署Web应用方法总结

    转载:http://m.blog.csdn.net/blog/u012516903/15741727 Tomcat部署Web应用方法总结 在Tomcat中部署Java Web应用程序有两种方式:静态部 ...

  2. FireMonkey支持的机型

    酷派5890(android 4.1.2) 从截图上看,正常.不知道为啥说不行.海信 T96(android 4.0.3) CPU 不支持 NEON.没辙.摩托罗拉XT885(android 4.0. ...

  3. Linux下安装、配置、启动Apache

    http://www.cnblogs.com/zhuque/archive/2012/11/03/2763352.html#

  4. Android Handler消息传递

    一.背景 出于性能优化考虑,Android的UI操作并不是线程安全的,这意味着如果有多个线程并发操作UI组件,可能导致线程安全问题.为了解决这个问题,Android制定了一条简单的原则:只允许UI线程 ...

  5. 转Java 回调函数的理解

    所谓回调,就是客户程序C调用服务程序S中的某个函数A,然后S又在某个时候反过来调用C中的某个函数B,对于C来说,这个B便叫做回调函数.例如Win32下的窗口过程函数就是一个典型的回调函数.一般说来,C ...

  6. UVA 11916 Emoogle Grid(同余模)

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. TC SRM 593 DIV1 250(dfs)

    这图最多3色就可以 搜2就行了 #include <iostream> #include<cstdio> #include<cstring> #include< ...

  8. SGU 275 To xor or not to xor (高斯消元)

    题目链接 题意:有n个数,范围是[0, 10^18],n最大为100,找出若干个数使它们异或的值最大并输出这个最大值. 分析: 一道高斯消元的好题/ 我们把每个数用二进制表示,要使得最后的异或值最大, ...

  9. ie下jquery ajax 80020101错误的解决方法

    <script language="javascript">    <!--    function checkAll(name,isCheck){       ...

  10. Codeforces_GYM Flight Boarding Optimization

    (ACM ICPC 2013–2014, NEERC, Northern Subregional Contest) Flight Boarding OptimizationInput file: fl ...