Regular Expression Matching

Implement regular expression matching with support for '.' and '*'.

'.' Matches any single character.
'*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be:
bool isMatch(const char *s, const char *p) Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true
 

 class Solution {
public:
bool isMatch(const char *s, const char *p) { if(s==NULL||p==NULL) return false;
if(*p=='\0'&&*s=='\0') return true;
if(*p=='\0'&&*s!='\0') return false; //如果模式串下一个字符为*
if(*(p+)=='*')
{
//循环比较当前字符与模式串字符是否相等
while((*s!='\0'&&*p=='.')||(*s==*p))
{
//防止这种情况出现:"aaa", "a*a"
if(isMatch(s,p+)) return true;
s++;
}
return isMatch(s,p+);
}
else if((*s!='\0'&&*p=='.')||*s==*p)
{
//如果当前元素相等,则开始匹配下一个元素
return isMatch(s+,p+);
}
return false;
}
};
 
 

【leetcode】Regular Expression Matching的更多相关文章

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

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

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

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

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

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

  4. 【leetcode刷题笔记】Regular Expression Matching

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

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

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

  6. [leetcode]10. Regular Expression Matching正则表达式的匹配

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

  7. LeetCode之Regular Expression Matching

    [题目描述] Implement regular expression matching with support for '.' and '*'. '.' Matches any single ch ...

  8. [LeetCode][Python]Regular Expression Matching

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

  9. 【LeetCode】44. Wildcard Matching (2 solutions)

    Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...

随机推荐

  1. Nginx + spawn-fcgi- Ubuntu中文

    Nginx - Ubuntu中文 页面 讨论 查看源代码 历史   导航 首页 最近更改 随机页面 页面分类 帮助 编辑 编辑指南 沙盒 新闻动态 字词处理 工具 链入页面 相关更改 特殊页面 打印版 ...

  2. C# 连接mongodb副本集+分片读写分离及学习资料

    一.副本集配置 搭建完毕,1台主实例.1台从实例.1台仲裁实例.mongodb建议副本集中的机器数量为奇数,即至少需要3台实例 二.副本集连接字符串 1.读 mongodb://secondary.c ...

  3. 关于MySQL的SLEEP(N)函数

    都知道通过在MySQL中执行select sleep(N)可以让此语句运行N秒钟: ? 1 2 3 4 5 6 7 mysql> select sleep(1); +----------+ | ...

  4. asp.net mvc 4 高级编程学习笔记:第三章 视图(1)

    1.基础规则 视图的职责是向用户提供用户界面. 视图位于View目录下:有普通的需要控制器渲染的视图,有局部视图,有布局视图等各种视图. 2.视图渲染 控制器默认情况下渲染与控制器同名的目录内的与Ac ...

  5. CF455C Civilization (并查集)

    CF456E Codeforces Round #260 (Div. 1) C Codeforces Round #260 (Div. 2) E http://codeforces.com/conte ...

  6. CF456C Boredom (DP)

    Boredom CF#260 div2 C. Boredom Codeforces Round #260 C. Boredom time limit per test 1 second memory ...

  7. Maven Eclipse (m2e) SCM connector for subclipse 1.10 (svn 1.8) 无法检测

    用新东西总是会有一些风险,尤其是相互的依赖和版本问题. 为了体验最新Eclipse Mars,Version: Mars Milestone 1 (4.5.0M1),Eclipse安装之后需要安装一些 ...

  8. "当前方法的代码已经过优化,无法计算表达式的值"的这个错误的解决方案!!!

    http://blog.useasp.net/archive/2012/09/12/how-to-debug-dotnet-framework-source-when-throw-the-code-o ...

  9. Bus Hound 的使用方法

    背景: 最近在研究USB相关协议,需要对USB数据进行抓取分析,Bus Hound是个非常赞的工具,在此将其使用方法记录下来,以备下次快速上手使用. 正文: 主界面如下: 首先关注菜单栏三个选项: C ...

  10. windows server2012和win8安装.netframework3.5失败问题及解决方法

    很多人安装windows server2012和Win8后都遇到了无法升级.NET Framework 3.5.1的问题,在线升级会遇到错误0x800F0906.这使得 91手机助手等很多软件无法运行 ...