我碰到下面的實現(https://discuss.leetcode.com/topic/40371/easy-dp-java-solution-with-detailed-explanation),但不安靜的理解它的工作原理如何 - 評論我怎麼迷路了:解釋我在網上找到的這段代碼給我?
boolean isMatch(String s, String p) {
if(s==null || p==null) {
return false;
}
// Why add an additional length to the string lengths?
boolean[][] dp = new boolean[s.length()+1][p.length()+1];
dp[0][0] = true;
// What’s the reason for this check? If p were to have ‘*’ at i=3, it would simply pass
for(int i=0; i<p.length(); i++) {
if(p.charAt(i)=='*' && dp[0][i-1]) {
dp[0][i+1] = true;
}
}
for(int i=0; i<s.length(); i++) {
for(int j=0; j<p.length(); j++) {
// Shouldn’t dp[i][j] just equal to true? Why set a boolean value to characters ahead?
if(p.charAt(j)=='.') {
dp[i+1][j+1] = dp[i][j];
}
// Same question as prior
if(p.charAt(j)==s.charAt(i)) {
dp[i+1][j+1] = dp[i][j];
}
if(p.charAt(j)=='*') {
// Not quiet understanding what the following checks are for and how they work
if(p.charAt(j-1)!=s.charAt(i) && p.charAt(j-1)!='.') {
dp[i+1][j+1] = dp[i+1][j-1];
} else {
dp[i+1][j+1] = (dp[i+1][j] || dp[i][j+1] || dp[i+1][j-1]);
}
}
}
}
return true;
}
預先感謝您,並一定要投上/接受的答案
我敢肯定你是如何丟失的是,一些塗料說_solve這riddle_接着就給出了基於語言的條件語句和數組的位置,即條件。 '如果p.charAt(J)== s.charAt(ⅰ):DP [i] [j] = DP [I-1] [j-1];'保持遠離這些盲目一無所知。 !花時間去完成一個目的,最終結果就是一些代碼。 – sln