您的位置:首页 > 其它

Leetcode_10

2016-09-11 16:42 267 查看


bool isMatch(char* s, char* p) {
int m = strlen(s);
int n= strlen(p);
bool f[m+1][n+1];
f[0][0] = true;//{} matches {}
f[0][1] = false;//{} matches nothing if p's length is 1
int i,j;
//initialize the i=0 and j=0 of the matrix of the dynamic programming
for(i = 1;i <= m;i++){
f[i][0] = false;
}
for(j = 2;j <= n;j++){
f[0][j] = ( '*'==p[j-1] && f[0][j-2] );
}
for(i = 1;i <= m;i++){
for(j = 1;j <= n;j++){
if(p[j-1] != '*'){
f[i][j] = ( f[i-1][j-1] && (s[i-1] == p[j-1] || '.' == p[j-1]) );
}
else{
f[i][j] = f[i][j-2] || ( s[i-1] == p[j-2] || '.' == p[j-2]) && f[i-1][j];
}
}
}
return f[m]
;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C leetcode