【一天一道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 ...
随机推荐
- 网站开发进阶(四十四)input type="submit" 和"button"的区别
网站开发进阶(四十四)input type="submit" 和"button"的区别 在一个页面上画一个按钮,有四种办法: 这就是一个按钮.如果你不写ja ...
- Android启动Activity
Android和java启动的区别 不同于使用 main() 方法启动应用的其他编程范例,Android 系统会通过调用对应于其生命周期中特定阶段的特定回调方法在 Activity 实例中启动代码.有 ...
- SQLite 创建表(http://www.w3cschool.cc/sqlite/sqlite-create-table.html)
SQLite 创建表 SQLite 的 CREATE TABLE 语句用于在任何给定的数据库创建一个新表.创建基本表,涉及到命名表.定义列及每一列的数据类型. 语法 CREATE TABLE 语句的基 ...
- 集合框架之Set接口
一个不包含重复元素的 collection.更确切地讲,set 不包含满足e1.equals(e2) 的元素对 e1 和 e2,并且最多包含一个 null 元素. 在所有构造方法以及 add.equa ...
- 18 UI美化自定义形状shape
自定义某个控件的形状 如 圆角 巨型 环形 : 在工程文件的新建 res/drawable/shape文件(以下键一个圆角) <?xml version="1.0" enco ...
- Python爬虫! 单爬,批量爬,这都不是事!
昨天做了一个煎蛋网妹子图的爬虫,个人感觉效果不错.但是每次都得重复的敲辣么多的代码(相比于Java或者其他语言的爬虫实现,Python的代码量可谓是相当的少了),就封装了一下!可以实现对批量网址以及单 ...
- Android官方推荐使用DialogFragment替换AlertDialog
DialogFragment是在Android3.0(API level 11)中引入的,它代替了已经不建议使用的AlertDialog. DialogFragment高效地封装和管理对话框的生命周期 ...
- Android初级教程启动定时器详解
本案例知识是:后台执行定时任务. Alarm机制: 一.创建LongRunningService类 package com.example.servicebestpractice; import ja ...
- springMVC源码分析--容器初始化(二)DispatcherServlet
在上一篇博客springMVC源码分析--容器初始化(一)中我们介绍了spring web初始化IOC容器的过程,springMVC作为spring项目中的子项目,其可以和spring web容器很好 ...
- SDK目录结构
android sdk里的各目录作用 AVD Manager.exe:虚拟机管理工具,用于建立和管理虚拟机. SDK Manager.exe:sdk管理工具,用于管理.下载sdk.sdk工具,能及扩展 ...