10. Regular Expression Matching字符串.*匹配
[抄题]:
Given an input string (s
) and a pattern (p
), 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).
Note:
s
could be empty and contains only lowercase lettersa-z
.p
could be empty and contains only lowercase lettersa-z
, and characters like.
or*
.
Example 1:
- Input:
- s = "aa"
- p = "a"
- Output: false
- Explanation: "a" does not match the entire string "aa".
Example 2:
- Input:
- s = "aa"
- p = "a*"
- Output: true
- Explanation: '*' means zero or more of the precedeng element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
Example 3:
- Input:
- s = "ab"
- p = ".*"
- Output: true
- Explanation: ".*" means "zero or more (*) of any character (.)".
Example 4:
- Input:
- s = "aab"
- p = "c*a*b"
- Output: true
- Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab".
Example 5:
- Input:
- s = "mississippi"
- p = "mis*is*p*."
- Output: false
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
想不到是dp:最值、可行、个数
[一句话思路]:
写不出程序,就尝试只写公式
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 先写一般情况,再写特殊情况,别丢了
[二刷]:
- 都是从1<x <=n开始写,请看新代码
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
前面的不能用,后果会遗传,是这道题和wildcard匹配的区别 dp[i][j] = dp[i][j - 2];
[复杂度]:Time complexity: O(n^2) Space complexity: O(n^2)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
- class Solution {
- public boolean isMatch(String s, String p) {
- //ini:dp[][], dp[0][0], dp[0][j]
- boolean[][] dp = new boolean[s.length() + 1][p.length() + 1];
- dp[0][0] = true;
- for (int j = 1; j <= p.length(); j++) {
- if (p.charAt(j - 1) == '*' && dp[0][j - 2] == true) dp[0][j] = true;
- }
- //for loop: 2 cases
- for (int i = 1; i <= s.length(); i++) {
- for (int j = 1; j <= p.length(); j++) {
- if (p.charAt(j - 1) == s.charAt(i - 1) ) {
- dp[i][j] = dp[i - 1][j - 1];
- }
- if (p.charAt(j - 1) == '.') {
- dp[i][j] = dp[i - 1][j - 1];
- }
- if (p.charAt(j - 1) == '*') {
- //*之前的不能用
- if (p.charAt(j - 2) != s.charAt(i - 1) && p.charAt(j - 2) != '.') {
- //前面的不能用,后果会遗传
- dp[i][j] = dp[i][j - 2];
- }else {
- //匹配多个,0个,1个
- dp[i][j] = dp[i - 1][j] || dp[i][j - 2] || dp[i][j - 1];
- }
- }
- }
- }
- //return
- return dp[s.length()][p.length()];
- }
- }
10. Regular Expression Matching字符串.*匹配的更多相关文章
- [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正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- 10. Regular Expression Matching正则表达式匹配
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 ...
- 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 ...
- 刷题10. Regular Expression Matching
一.题目说明 这个题目是10. Regular Expression Matching,乍一看不是很难. 但我实现提交后,总是报错.不得已查看了答案. 二.我的做法 我的实现,最大的问题在于对.*的处 ...
- 《LeetBook》leetcode题解(10): Regular Expression Matching——DP解决正则匹配
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- [leetcode]10. Regular Expression Matching正则表达式的匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
随机推荐
- Oracle 索引的失效和重建
查询指定表的索引 SELECT T1.TABLE_NAME, T1.INDEX_NAME, T1.INDEX_TYPE, T1.UNIQUENESS, T1.TABLE_OWNER, T1.STATU ...
- ASP.NET中服务器控件的生命周期
服务器控件的生命周期是创建服务器控件最重要的概念.作为开发人员,必须对服务器控件生命周期深刻理解.当然,这不是一朝一夕就可以做到的.对于学习控件开发技术的初学者,可以不必掌握得非常详细深入,只需对服务 ...
- 洛谷P2835 刻录光盘
传送门 题目大意:有光盘可以传着看,问最少从哪几个人分发,能全部传一遍. 题解:缩点后求入度为0的点的个数 代码: #include<iostream> #include<cstdi ...
- 脚本工具---自动解析mysql建表语句,生成sqlalchemy表对象声明
常规建表语句: CREATE TABLE `test_table` ( `id` int(11) NOT NULL, `name` char(64) NOT NULL, `password` char ...
- 不得不注意tornado多进程部署的副作用
tornado多进程启动时,采用的是fork的方式. 一个现有进程可以调用fork函数创建一个新进程.由fork创建的新进程被称为子进程(child process).fork函数被调用一次但返回两次 ...
- numpy之初识ndarray
Numpy ndarray numpy的最重要特点就是其N维数组对象(ndarray). ndarray的可以对整块数据执行数学运算,语法与标量元素的元素的运算一致. 如: import numpy ...
- MySQL中的各种引擎
数据库中的存储引擎其实是对使用了该引擎的表进行某种设置,数据库中的表设定了什么存储引擎,那么该表在数据存储方式.数据更新方式.数据查询性能以及是否支持索引等方面就会有不同的“效果”.在MySQL数据库 ...
- (转)Oracle 临时表用法
本文转载自:http://www.iteye.com/topic/371390 ORACLE的临时表在应用系统中有很大的作用,它可以让用户只能够操作各自的数据中而互不干扰,不用担心会破坏或影响其他SE ...
- Unity Shader入门教程(三)自制光照模型
光照模型的概念目前还不明晰,因为笔者也是一个初学者,所以请小心对待笔者介绍的内容.笔者认为光照模型是规定光照算法的模型,比如说前面提到的Lambert光照模型,规定了材质表面的光线的表达式为 环境光+ ...
- Quartz 用 cron 表达式存放执行计划
Quartz 用 cron 表达式存放执行计划.引用了 cron 表达式的 CronTrigger 在计划的时间里会与 job 关联上. 1.Quartz cron 表达式支持七个域如下: 名称 是否 ...