https://leetcode-cn.com/problems/regular-expression-matching/solution/zheng-ze-biao-da-shi-pi-pei-by-leetcode-solution/
class Solution {
public boolean isMatch(String s, String p) {
int m=s.length();int n=p.length();
boolean[][] dp=new boolean[m+1][n+1];
dp[0][0]=true;
// for (int i = 1; i <= m; i++) {
// dp[i][0]=false;
// }
int flag=0;
for (int i = 1; i <= n; i++) {
if(flag==1&&p.charAt(i-1)=='*'){
dp[0][i]=true;
flag=0;
}else {
flag++;
dp[0][i]=false;
}
}
for (int i = 1; i <= m; i++) {
char cs = s.charAt(i-1);
for (int j = 1; j <= n; j++) {
char cp = p.charAt(j - 1);
if(cp!='*'){
if(matches(cs,cp)){
dp[i][j]=dp[i-1][j-1];
}
else{
dp[i][j]=false;
}
}else {
if(matches(cs,p.charAt(j-2))){
dp[i][j]=(dp[i-1][j]||dp[i][j-2]);
}else{
dp[i][j]=dp[i][j-2];
}
}
}
}
return dp[m][n];
}
boolean matches(char s,char p){
if(s==p||p=='.')
return true;
else
return false;
}
}
注意边界值的处理