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]
}
相关推荐
文丑颜不良啊18 小时前
LeetCode2821~2840题目解析
java·leetcode
Pniubi1 天前
力扣24. 两两交换链表中的节点(递归)
算法·leetcode·链表
文丑颜不良啊1 天前
LeetCode2741~2760题目解析
java·leetcode
俊俊几年的1 天前
动态规划(Dynamic Programming)详解:从入门到 LeetCode Hot 100
linux·c++·算法·leetcode·嵌入式·动态规划·c
程序员Query1 天前
day01-认识Go语言 + golang环境的安装
开发语言·后端·golang
希望我万事顺遂2 天前
力扣hot100-滑动窗口最大值
数据结构·算法·leetcode
喵了几个咪2 天前
GoWind Flow 拆解:一个 Go 工作流引擎与它的双模式设计器
开发语言·人工智能·后端·golang
codeejun2 天前
每日一Go·MySQL-14、慢 SQL 产生原因全解
sql·mysql·golang
是main不是漫2 天前
【双指针|LeetCode1089 复写零】巧用逆向思路,破解原地覆盖陷阱
数据结构·算法·leetcode
Pniubi3 天前
力扣76.最小覆盖子串
算法·leetcode·职场和发展