0010.Regular Expression Matching(H)
jjc
. Regular Expression Matching(hard) 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 letters a-z. p could be empty and contains only lowercase letters a-z, and characters like . or *. Example : Input: s = "aa" p = "a" Output: false Explanation: "a" does not match the entire string "aa". Example : 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 : Input: s = "ab" p = ".*" Output: true Explanation: ".*" means "zero or more (*) of any character (.)". Example : Input: s = "aab" p = "c*a*b" Output: true Explanation: c can be repeated times, a can be repeated time. Therefore it matches "aab". Example : Input: s = "mississippi" p = "mis*is*p*." Output: false Accepted ,,,
class Solution_S4ms { public: bool isMatch(string s, string p) { ](); ; i < s.length() + ; ++i) { dp[i] = ]; memset(dp[i], , p.length()+); } dp[s.size()][p.size()] = true; ; i--){ ; j >= ; j--) { bool first_match = (i < s.length() && (p[j] == s[i] || p[j] == '.')); < p.length() && p[j+] == '*') { dp[i][j] = dp[i][j+] || first_match && dp[i+][j]; } else { dp[i][j] = first_match && dp[i+][j+]; } } } ][]; } }; class Solution_S8ms { public: bool isMatch(string s, string p) { int m = s.size(), n = p.size(); vector<vector<, vector<, false)); dp[][] = true; ; i <= m; ++i) { ; j <= n; ++j) { && p[j - ] == '*') { dp[i][j] = dp[i][j - ] || (i > && (s[i - ] == p[j - ] || p[j - ] == ][j]); } else { dp[i][j] = i > && dp[i - ][j - ] && (s[i - ] == p[j - ] || p[j - ] == '.'); } } } return dp[m][n]; } };
/* https://www.cnblogs.com/grandyang/p/4461713.html * dp[i][j]表示s[0,i)和p[0,j)是否match 1. P[i][j] = P[i - 1][j - 1], if p[j - 1] != '*' && (s[i - 1] == p[j - 1] || p[j - 1] == '.'); 2. P[i][j] = P[i][j - 2], if p[j - 1] == '*' and the pattern repeats for 0 times; 3. P[i][j] = P[i - 1][j] && (s[i - 1] == p[j - 2] || p[j - 2] == '.'), if p[j - 1] == '*' and the pattern repeats for at least 1 times.*/ class Solution_grandyang { public: bool isMatch(string s, string p) { int m = s.size(), n = p.size(); vector<vector<, vector<, false)); dp[][] = true; ; i <= m; ++i) { ; j <= n; ++j) { && p[j - ] == '*') { dp[i][j] = dp[i][j - ] || (i > && (s[i - ] == p[j - ] || p[j - ] == ][j]); } else { dp[i][j] = i > && dp[i - ][j - ] && (s[i - ] == p[j - ] || p[j - ] == '.'); } } } return dp[m][n]; } };
0010.Regular Expression Matching(H)的更多相关文章
- 10. Regular Expression Matching[H]正则表达式匹配
题目 Given an input string(s) and a pattern(p), implement regular expression matching with support for ...
- 刷题10. Regular Expression Matching
一.题目说明 这个题目是10. Regular Expression Matching,乍一看不是很难. 但我实现提交后,总是报错.不得已查看了答案. 二.我的做法 我的实现,最大的问题在于对.*的处 ...
- [LeetCode] Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- [LeetCode] 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...
- No.010:Regular Expression Matching
问题: Implement regular expression matching with support for '.' and '*'.'.' Matches any single charac ...
- Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【leetcode】Regular Expression Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
- 【leetcode】Regular Expression Matching (hard) ★
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 66. Regular Expression Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
随机推荐
- Spring框架 IOC注解
Spring框架的IOC之注解方式的快速入门 1. 步骤一:导入注解开发所有需要的jar包 * 引入IOC容器必须的6个jar包 * 多引入一个:Spring ...
- usa单位换算
1.温度换算 摄氏度 C = 5/9(F-32) ≍ (F-32)/1.8 (F为华氏温度值) 华氏度 F = 1.8C + 32 (C为摄氏温度值) 3.重量换算 1品脱(pint) ≍ ...
- LeetCode 311. Sparse Matrix Multiplication
原题链接在这里:https://leetcode.com/problems/sparse-matrix-multiplication/description/ 题目: Given two sparse ...
- [学习笔记]约数&欧拉函数
约数 一.概念 约数,又称因数.整数a除以整数b(b≠0) 除得的商正好是整数而没有余数,我们就说a能被b整除,或b能整除a.a称为b的倍数,b称为a的约数. 二.性质 1.整数唯一分解 1)定义 对 ...
- HiveQL 数据装在与导出
一.向管理表中装载数据 1.向表中装载数据load 1)load语法 2)LOCAL 指的是操作系统的文件路径,否则默认为HDFS的文件路径 3)overwrite关键字 如果用户指定了overwr ...
- TPS与QPS,以及GMV
TPS是指每秒处理事务的个数,处理的载体可以是单台服务器,也可以是一个服务器集群. 例如:下单接口,一秒内,下单完成次数为1000,则下单接口总 tps = 1000,共有10台服务器提供下单服务,单 ...
- AttributeError: module 'tensorflow' has no attribute 'set_random_seed'
anaconda3 python3.7 安装好tensorflow 后出现上面的问题,原因是安装的tensorflow版本是2.0的,所以使用以前的代码tensorflow中的函数不兼容.
- UOJ272. 【清华集训2016】石家庄的工人阶级队伍比较坚强 [FWT]
UOJ 思路 很容易想到\(O(3^{3m}\log T)\)的暴力大矩乘,显然过不了. 我们分析一下每次转移的性质.题目给的转移方程是填表法,我们试着改成刷表法看看-- 发现好像没啥用. 注意到游戏 ...
- php 压缩文件
<?php $zip = new ZipArchive; $myfile = fopen("test.zip", "w"); chmod(); if ($ ...
- Cisco实验图