LeetCode.10.正则表达式匹配
这个比较困难,我参考了网上的代码: https://leetcode-cn.com/problems/regular-expression-matching/solution/di-gui-dong-tai-gui-hua-zhu-xing-jie-shi-python3-b/ 思路: 动态规划, 沿着匹配串和字符串构成矩阵的对角线传递状态 1. 状态矩阵的首行与首列对应于空字符与空匹配符 2. 对角线意味着匹配串是否匹配对应的字符串 具体代码以下: ''' p.charAt(j) == s.charAt(i) : dp[i][j] = dp[i-1][j-1] If p.charAt(j) == '.' : dp[i][j] = dp[i-1][j-1]; If p.charAt(j) == '*': here are two sub conditions: //in this case, a* only counts as empty, otherwise is not match - if p.charAt(j-1) != s.charAt(i) : dp[i][j] = dp[i][j-2] - if p.charAt(j-1) == s.charAt(i) or p.charAt(i-1) == '.': dp[i][j] = dp[i-1][j] //in this