class Solution {
public boolean isMatch(String s, String p) {
s=" "+s;
p=" "+p;
int sl=s.length();
int jl=p.length();
boolean [][]dp=new boolean[sl+1][jl+1];
dp[0][0]=true;
for(int i=1;i<=sl;i++)
{
for(int j=1;j<=jl;j++)
{
if(s.charAt(i-1)==p.charAt(j-1)||p.charAt(j-1)=='.')
{
dp[i][j]=dp[i-1][j-1];
}
else if(p.charAt(j-1)=='*')
{
if(s.charAt(i-1)!=p.charAt(j-2)&&p.charAt(j-2)!='.')
{
dp[i][j]=dp[i][j-2];
}
else
{
dp[i][j]=dp[i][j-2]||dp[i-1][j]||dp[i][j-1];
}
}
}
}
return (dp[sl][jl]);
}
}
对于21-30行的代码难以理解。
能否把for循环改成while使得i->s.length(),j->p.length().类型的交替循环,而不是for循环类型的嵌套。
对于动态规划不熟悉。