前言

本文主要提供三种不同的解法,分别是利用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特性、动态规划、递归相关推荐

  1. 【重点 递归 动态规划 正则表达式匹配】LeetCode 10. Regular Expression Matching

    LeetCode 10. Regular Expression Matching 本博客参考:http://www.cnblogs.com/grandyang/p/4461713.html 详细解析见 ...

  2. 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] ...

  3. leetcode 10 Regular Expression Matching

    题目连接 https://leetcode.com/problems/regular-expression-matching/ Regular Expression Matching Descript ...

  4. LeetCode 10 Regular Expression Matching(字符串匹配)

    题目链接 https://leetcode.com/problems/regular-expression-matching/?tab=Description   '.' Matches any si ...

  5. LeetCode:10. Regular Expression Matching

    老大难问题,终于算是理解了. 首先状态定义不难写(哈哈): dp[i][j]: s[0->i-1] p[0->j-1],它们是不是符合. 难的是状态转移方程(又一次验证): 1.如果s[i ...

  6. 【LeetCode】10. Regular Expression Matching

    题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...

  7. Leetcode Q10: Regular Expression Matching

    题目10: (该题目拿到手没什么特别好的思路,从网上看的别人的解法,然后写了下自己的理解,需要常回顾) Implement regular expression matching with suppo ...

  8. Q10 Regular Expression Matching

    Given an input string (s) and a pattern §, implement regular expression matching with support for '. ...

  9. Regular Expression Matching

    正则匹配 Regular Expression Matching Implement regular expression matching with support for '.' and '*'. ...

最新文章

  1. 剑指offer:面试题04. 二维数组中的查找
  2. 外媒:苹果自动驾驶裁员表明其AI战略发生巨变
  3. Java7中的switch支持String的实现细节
  4. 生产上如何设置线程池参数?拒绝策略怎么配?|| Executors 中 JDK 给你提供了,为什么不用??
  5. ITK:指定区域裁剪图像
  6. Netscreen防火墙常用命令-管理篇
  7. django构建网页_如何使用Django构建照片供稿
  8. VC++2010配置使用MySQL5.6
  9. JS基础_JS基础语法
  10. Sqlit--学习教程(基本操作1)
  11. 141.PHP 对象赋值
  12. cartographer探秘第四章之代码解析(一) --- SLAM处理过程 --- 文章索引
  13. 输入法相关的使用(跳转)
  14. K3CLOUD 常用数据表
  15. python中sqrt函数用法_Python : sqrt() 函数
  16. 企业实施PDM能解决这些问题
  17. 【2021.08】python会员数据化运营task01
  18. CSS3篮球场热力区域图
  19. 为什么MacBook连接不上阿里云服务器
  20. TUV南德与重庆赛宝于四川签署合作协议并联合举办多国认证研讨会

热门文章

  1. 正向最大匹配 和逆向最大匹配对比比较
  2. cuSPARSELt开发NVIDIA Ampere结构化稀疏性
  3. 部署通用基础设施, 满足顶级 SLA 要求
  4. 与现代传感器的接口:轮询ADC驱动程序
  5. Thrift的服务器和客户端Python案例
  6. 阿里云https认证
  7. Python 判断本地python 本地版本2x or 3x
  8. Ubuntu系统执行shell 脚本的方法
  9. Android 属性动画(Property Animation) ValueAnimator 的介绍
  10. RecyclerView 滑动显示返回按钮,点击返回到顶部