Leetcode-10 正则表达式匹配
原题链接 https://leetcode-cn.com/problems/regular-expression-matching/
1 2
| func isMatch(s string, p string) bool { }
|
题解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| func isMatch(s string, p string) bool { row, col := len(s), len(p)
dp := make([][]bool, row+1) for i := 0; i < len(dp); i++ { dp[i] = make([]bool, col+1) }
for j := 0; j < col+1; j++ { if j == 0 { dp[0][0] = true } else if p[j-1] == '*' { dp[0][j] = dp[0][j-2] } }
for i := 1; i < row+1; i++ { for j := 1; j < col+1; j++ { if p[j-1] == '*' { if s[i-1] == p[j-2] || p[j-2] == '.' { dp[i][j] = dp[i][j-2] || dp[i-1][j] } else { dp[i][j] = dp[i][j-2] } } else if s[i-1] == p[j-1] || p[j-1] == '.' { dp[i][j] = dp[i-1][j-1] } } }
return dp[row][col] }
|
Github: https://github.com/Junedayday/code_reading
Blog: http://junes.tech/
Bilibili: https://space.bilibili.com/293775192
公众号: golangcoding