Golang | Leetcode Golang题解之第10题正则表达式匹配

题目:

题解:

Go 复制代码
func isMatch(s string, p string) bool {
    m, n := len(s), len(p)
    matches := func(i, j int) bool {
        if i == 0 {
            return false
        }
        if p[j-1] == '.' {
            return true
        }
        return s[i-1] == p[j-1]
    }

    f := make([][]bool, m + 1)
    for i := 0; i < len(f); i++ {
        f[i] = make([]bool, n + 1)
    }
    f[0][0] = true
    for i := 0; i <= m; i++ {
        for j := 1; j <= n; j++ {
            if p[j-1] == '*' {
                f[i][j] = f[i][j] || f[i][j-2]
                if matches(i, j - 1) {
                    f[i][j] = f[i][j] || f[i-1][j]
                }
            } else if matches(i, j) {
                f[i][j] = f[i][j] || f[i-1][j-1]
            }
        }
    }
    return f[m][n]
}
相关推荐
tachibana229 分钟前
hot100 回文链表(234)
java·网络·数据结构·leetcode·链表
wabs6663 小时前
关于动态规划【力扣1143.最长公共子序列的思考】
算法·leetcode·动态规划
剑挑星河月3 小时前
54.螺旋矩阵
java·算法·leetcode·矩阵
笨笨没好名字5 小时前
Leetcode刷题python3版第一周(下)
linux·算法·leetcode
想你依然心痛5 小时前
AtomCode在后端开发中的实战体验:Go微服务从零搭建
开发语言·微服务·golang
王老师青少年编程6 小时前
2026年6月GESP真题及题解(C++五级):排排坐
c++·题解·真题·gesp·五级·2026年6月·排排坐
开发小程序的之朴6 小时前
认识安企CMS - 系统概述
nginx·golang·系统架构
雨师@6 小时前
go语言项目--实例化(图书管理)--005
开发语言·后端·golang
Vect__7 小时前
Go 数据结构 slice 深度剖析
开发语言·数据结构·golang
geovindu7 小时前
go: Functional Options Pattern
开发语言·后端·设计模式·golang·函数式选项模式’·惯用法模式