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]
}
相关推荐
枫景Maple10 小时前
LeetCode 2297. 跳跃游戏 VIII(中等)
算法·leetcode
roman_日积跬步-终至千里13 小时前
【Go语言基础【9】】字符串格式化与输入处理
golang
緈福的街口13 小时前
【leetcode】3. 无重复字符的最长子串
算法·leetcode·职场和发展
小刘不想改BUG16 小时前
LeetCode 70 爬楼梯(Java)
java·算法·leetcode
比特森林探险记17 小时前
Go 中的 Map 与字符处理指南
c++·算法·golang
比特森林探险记17 小时前
Go 中 map 的双值检测写法详解
java·前端·golang
哆啦A梦158818 小时前
在golang中如何将已安装的依赖降级处理,比如:将 go-ansible/[email protected] 更换为 go-ansible/@v1.1.7
开发语言·golang·ansible
LanLance18 小时前
ES101系列09 | 运维、监控与性能优化
java·运维·后端·elasticsearch·云原生·性能优化·golang
sz66cm19 小时前
LeetCode刷题 -- 542. 01矩阵 基于 DFS 更新优化的多源最短路径实现
leetcode·矩阵·深度优先