LeetCode 10. Regular Expression Matching python特性、动态规划、递归
前言
本文主要提供三种不同的解法,分别是利用python的特性、动态规划、递归方法解决这个问题
使用python正则属性
import reclass Solution2:# @return a booleandef isMatch(self, s, p):return re.match('^' + p + '$', s) != None
使用递归方法
class Solution(object):def isMatch(self, text, pattern):if not pattern:return not textfirst_match = bool(text) and pattern[0] in {text[0], '.'}if len(pattern) >= 2 and pattern[1] == '*':return (self.isMatch(text, pattern[2:]) orfirst_match and self.isMatch(text[1:], pattern))else:return first_match and self.isMatch(text[1:], pattern[1:])
使用动态规划
动态规划与上面的递归的区别就在于用空间换取了时间的复杂度
class Solution1(object):def isMatch(self, text, pattern):memo = {}def dp(i, j):if (i, j) not in memo:if j == len(pattern):ans = i == len(text)else:first_match = i < len(text) and pattern[j] in {text[i], '.'}if j+1 < len(pattern) and pattern[j+1] == '*':ans = dp(i, j+2) or first_match and dp(i+1, j)else:ans = first_match and dp(i+1, j+1)memo[i, j] = ansreturn memo[i, j]return dp(0, 0)
后记
这题在leetcode中算是一道难题,有很多值得品味的地方,后面将继续深入研究
参考文档
https://blog.csdn.net/gatieme/article/details/51049244
https://leetcode.com/problems/regular-expression-matching/solution/#
LeetCode 10. Regular Expression Matching python特性、动态规划、递归相关推荐
- 【重点 递归 动态规划 正则表达式匹配】LeetCode 10. Regular Expression Matching
LeetCode 10. Regular Expression Matching 本博客参考:http://www.cnblogs.com/grandyang/p/4461713.html 详细解析见 ...
- LeetCode 10. Regular Expression Matching / 44. Wildcard Matching
10. Regular Expression Matching 经典DP题目,比较复杂,需要多复习. dp[i][j] 表示 s 下标0~i,p 下标0~j 是否能够匹配 dp[i-1][j-1] ...
- leetcode 10 Regular Expression Matching
题目连接 https://leetcode.com/problems/regular-expression-matching/ Regular Expression Matching Descript ...
- LeetCode 10 Regular Expression Matching(字符串匹配)
题目链接 https://leetcode.com/problems/regular-expression-matching/?tab=Description '.' Matches any si ...
- LeetCode:10. Regular Expression Matching
老大难问题,终于算是理解了. 首先状态定义不难写(哈哈): dp[i][j]: s[0->i-1] p[0->j-1],它们是不是符合. 难的是状态转移方程(又一次验证): 1.如果s[i ...
- 【LeetCode】10. Regular Expression Matching
题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- Leetcode Q10: Regular Expression Matching
题目10: (该题目拿到手没什么特别好的思路,从网上看的别人的解法,然后写了下自己的理解,需要常回顾) Implement regular expression matching with suppo ...
- Q10 Regular Expression Matching
Given an input string (s) and a pattern §, implement regular expression matching with support for '. ...
- Regular Expression Matching
正则匹配 Regular Expression Matching Implement regular expression matching with support for '.' and '*'. ...
最新文章
- 剑指offer:面试题04. 二维数组中的查找
- 外媒:苹果自动驾驶裁员表明其AI战略发生巨变
- Java7中的switch支持String的实现细节
- 生产上如何设置线程池参数?拒绝策略怎么配?|| Executors 中 JDK 给你提供了,为什么不用??
- ITK:指定区域裁剪图像
- Netscreen防火墙常用命令-管理篇
- django构建网页_如何使用Django构建照片供稿
- VC++2010配置使用MySQL5.6
- JS基础_JS基础语法
- Sqlit--学习教程(基本操作1)
- 141.PHP 对象赋值
- cartographer探秘第四章之代码解析(一) --- SLAM处理过程 --- 文章索引
- 输入法相关的使用(跳转)
- K3CLOUD 常用数据表
- python中sqrt函数用法_Python : sqrt() 函数
- 企业实施PDM能解决这些问题
- 【2021.08】python会员数据化运营task01
- CSS3篮球场热力区域图
- 为什么MacBook连接不上阿里云服务器
- TUV南德与重庆赛宝于四川签署合作协议并联合举办多国认证研讨会