【leetcode】Regular Expression Matching
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的更多相关文章
- 【leetcode】Regular Expression Matching (hard) ★
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- 【JAVA、C++】LeetCode 010 Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【leetcode刷题笔记】Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- LeetCode (10): Regular Expression Matching [HARD]
https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...
- [leetcode]10. Regular Expression Matching正则表达式的匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- LeetCode之Regular Expression Matching
[题目描述] Implement regular expression matching with support for '.' and '*'. '.' Matches any single ch ...
- [LeetCode][Python]Regular Expression Matching
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...
- 【LeetCode】44. Wildcard Matching (2 solutions)
Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...
随机推荐
- Effective Objective-C 2.0 — 第一条:了解Objective-C语言的起源
第一条: 了解Objective-C语言的起源 由Smalltalk演化而来,消息型语言的鼻祖(messaging structure)而非 (function calling)函数调用 //Mess ...
- js实现点击增加文本输入框
html代码: <ul id="ulid21" > <li id="li11" >问卷选项设置:</li> </ul& ...
- array_flip() array_merge() array+array的使用总结
array_flip(array); //传递一个数组参数,对该数组的键.值进行翻转 例如: $a = array( 'a', 'b', 'c' ); print_r(array_flip($a)); ...
- Entity Framework实例详解
Entity Framework Code First的默认行为是使用一系列约定将POCO类映射到表.然而,有时候,不能也不想遵循这些约定,那就需要重写它们.重写默认约定有两种方式:Data Anno ...
- WCF-复合类型使用;传输图片
一:WCF服务端 IService1.cs中: public interface IService1 { [OperationContract] [WebInvoke(Method = "P ...
- 老项目的#iPhone6与iPhone6Plus适配#LaunchImage适配
本文永久地址为 http://www.cnblogs.com/ChenYilong/p/4020384.html,转载请注明出处. Evernote印象笔记链接:https://www.everno ...
- ajax浅析---UpdatePanel
使用UpdatePanel控件 UpdatePanel可以用来创建丰富的局部更新Web应用程序,它是ASP.NET 2.0 AJAX Extensions中很重要的一个控件,其强大之处在于不用编写任何 ...
- Linux 下新增虚拟内存
问题描述,电脑内存1G.在启用elasticsearch的时候,报错 Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memor ...
- IDEA之创建不了.java文件解决
1.问题:在IDEA中新建的maven项目,无法创建.java文件 从上图看出,在new对应的栏目中没有java class选项 2.解决 这是因为maven的配置问题 应该如下: 注:如果这样还不行 ...
- C\C++ sizeof 陷阱&&总结
今天使用动态数组,本来想通过sizeof 获取动态数据,结果出现了错误. 先对自己做个测试,能做出下面这个题目,并做出合理解释,可以不用往下看了. ][]; cout<< cout< ...