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]
}
相关推荐
Shaoshing19 小时前
LeetCode 921.使括号括号有效的最少添加
java·算法·leetcode·职场和发展
圣保罗的大教堂21 小时前
leetcode 3884. 双端字符匹配 简单
leetcode
SailingCoder1 天前
Go 线上偶发数据错乱:值拷贝、slice、并发 map 的识别与规避
开发语言·后端·golang
codeejun1 天前
每日一Go·MySQL-28、账户转账事务设计
开发语言·mysql·golang
Shaoshing1 天前
LeetCode100 76.最小覆盖字串
java·算法·leetcode
microrain1 天前
设备说 128.7,平台存了 100
物联网·golang·物模型·sagooiot
Navigator_Z1 天前
LeetCode //C - 1331. Rank Transform of an Array
c语言·算法·leetcode
Tisfy1 天前
LeetCode 1202.交换字符串中的元素:分组排序(连通图)
算法·leetcode·字符串·bfs·题解·排序·连通图
利威尔xu2 天前
Go map 并发十连问:多 goroutine 同时读写触发 fatal error,进程直接崩溃
开发语言·golang
喵了几个咪2 天前
GoWind Admin 拆解:智能问数——从一句自然语言到一张可信图表
ai·golang·llm·智能问数·gowind