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]
}
相关推荐
吃着火锅x唱着歌5 小时前
LeetCode 3885.设计事件管理器
算法·leetcode·职场和发展
土司大王5 小时前
LeetCode hto100——字母异位词分组
java·算法·leetcode
土司大王6 小时前
LeetCode hot100——两数之和
数据结构·算法·leetcode
Navigator_Z7 小时前
LeetCode //C - 1200. Minimum Absolute Difference
c语言·算法·leetcode
wabs6667 小时前
关于字符串【力扣344.反转字符串的思考】
数据结构·算法·leetcode
Nil2088 小时前
leetcode 73矩阵置0
算法·leetcode·矩阵
报错小能手8 小时前
Go 语言结构 基础语法
开发语言·后端·golang
剩下了什么9 小时前
go语言 Ctx:「错误三:在 Context 中存储可变值」
开发语言·后端·golang
运维开发笔记10 小时前
3.7 Go panic 与 recover 学习笔记
golang
JavaPub-rodert10 小时前
我把 OpenAI 协议塞进了 Go 工具库:go-commons 开始支持 AI 了
开发语言·人工智能·golang