【一天一道LeetCode】#10. Regular Expression Matching
一天一道LeetCode系列
(一)题目
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
(二)解题
本题的意思是实现正则表达式的判断函数。
tips:评级为hard的题还真是难做!
考虑到aaaaaab和a*b,这种无法判断*的结束位置,需要用到动态规划来求解。
分为以下两种情况:
case 1:p[j+1] !=’*’时,无论是是s[i] == p[j]还是p[j]==’.’,状态转移方程均为dfs[i][j] = dfs[i+1][j+1];
case 2:p[j+1] == ‘‘时,这个时候就需要判断匹配的结束位置。
while(*s == *p || *s != '\0' && *p == '.')
{
if(Match(s,p+2)) return true;
s++;
}
结束位置找到以后状态转移方程为:dfs[i][j] = dfs[i][j+2];
接下来看代码:
class Solution {
public:
bool isMatch(string s, string p) {
Match((const char *)&s[0],(const char *)&p[0]);
}
bool Match(const char *s,const char *p)
{
if(*p == '\0') return *s == '\0';
if(*(p+1) == '*')
{
while(*s == *p || *s != '\0' && *p == '.')
{
if(Match(s,p+2)) return true;
s++;
}
return Match(s,p+2);
}
else
{
if(*s == *p ||*s != '\0' && *p == '.')
{
return Match(s+1,p+1);
}
return false;
}
}
};
【一天一道LeetCode】#10. Regular Expression Matching的更多相关文章
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- Leetcode 10. Regular Expression Matching(递归,dp)
10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...
- leetcode 10. Regular Expression Matching 、44. Wildcard Matching
10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...
- [LeetCode] 10. Regular Expression Matching 正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- LeetCode (10): Regular Expression Matching [HARD]
https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...
- [LeetCode] 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...
- Java [leetcode 10] Regular Expression Matching
问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
- [leetcode]10. Regular Expression Matching正则表达式的匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- 蜗牛慢慢爬 LeetCode 10. Regular Expression Matching [Difficulty: Hard]
题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single charac ...
- [LeetCode] 10. Regular Expression Matching ☆☆☆☆☆
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
随机推荐
- NLP系列(2)_用朴素贝叶斯进行文本分类(上)
作者:龙心尘 && 寒小阳 时间:2016年1月. 出处: http://blog.csdn.net/longxinchen_ml/article/details/50597149 h ...
- 安卓 LayoutInflater参数作用
方法重载1 public View inflate (int resource, ViewGroup root, boolean attachToRoot) 方法重载2 public View inf ...
- Android开发技巧——定制仿微信图片裁剪控件
拍照--裁剪,或者是选择图片--裁剪,是我们设置头像或上传图片时经常需要的一组操作.上篇讲了Camera的使用,这篇讲一下我对图片裁剪的实现. 背景 下面的需求都来自产品. 裁剪图片要像微信那样,拖动 ...
- cassandra 概述
摘要 本篇文章主要是介绍cassandra与其他NoSQL的区别以及自身的特点与应用场景.在关系数据库我们没必要选择数据库,通常需要适配oracle/mysql/sql server/db2 等多种数 ...
- Android艺术开发探索第三章——View的事件体系(上)
Android艺术开发探索第三章----View的事件体系(上) 我们继续来看这本书,因为有点长,所以又分了上下,你在本片中将学习到 View基础知识 什么是View View的位置参数 Motion ...
- Retrofit2.0+RxJava+Dragger2实现不一样的Android网络架构搭建
Tamic :csdn http://blog.csdn.net/sk719887916 众所周知,手机APP的核心就在于调用后台接口,展示相关信息,方便我们在手机上就能和外界交互.所以APP中网络框 ...
- thinkpad彻底消除"触摸键盘"图标
输入“服务”,进入“查看本地服务”,找到“Touch Keyboard and Handwriting Panel Service”, 将其启动类型改为“禁用”,这样的话重启电脑之后也不会自动启动这触 ...
- 23 服务的创建Demo1
结构 MainActivity.java package com.qf.day23_service_demo1; import android.app.Activity; import android ...
- 【移动开发】plurals
不同的语言对数量的语法规定有不同的规则.在英语里面,例如,1是特例.我们会直接写1book,而针对一个以上的我们会在book后加复数形式.这种区别对单数和复数来说是很普遍的,但是其他的语言做了更好的区 ...
- [cacti]nginx+php+cacti+mysql+php-fpm 安装小记
网上教程很多,但是nginx不太多,下面安装时候主要参考的篇文章: http://54im.com/linux/linux-cacti-cn-install.html http://www.tecmi ...