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]
}
相关推荐
不爱编程的小陈14 分钟前
深入解析 Go 网络 I/O 的底层引擎:从 epoll 到 netpoll
服务器·网络·golang
人道领域1 小时前
【LeetCode刷题日记】47.全排列Ⅱ
java·开发语言·算法·leetcode
Navigator_Z1 小时前
LeetCode //C - 1095. Find in Mountain Array
c语言·算法·leetcode
何以解忧,唯有..4 小时前
Go 语言数据类型详解:从基础到复合类型
开发语言·golang·mfc
踏着七彩祥云的小丑4 小时前
Go学习第7天:Map集合 + 递归函数 + 类型转换
开发语言·学习·golang·go
何以解忧,唯有..4 小时前
Go语言变量的声明方式详解
开发语言·后端·golang
寂夜了无痕5 小时前
Go 多版本管理工具G 保姆级安装配置教程
golang·go多版本管理
张忠琳5 小时前
【Go 1.26.4】Golang Slice 深度解析
开发语言·后端·golang
暖阳华笺6 小时前
【数据结构与算法】哈希专题
数据结构·c++·算法·leetcode·哈希算法
AKA__Zas6 小时前
芝士算法(滑动窗口片 2.0)
java·算法·leetcode·学习方法