Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 向我解释我在互联网上找到的代码?_Java - Fatal编程技术网

Java 向我解释我在互联网上找到的代码?

Java 向我解释我在互联网上找到的代码?,java,Java,我遇到了以下实现(),但并不完全理解它是如何工作的——对我是如何迷路的发表了评论: 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.len

我遇到了以下实现(),但并不完全理解它是如何工作的——对我是如何迷路的发表了评论:

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;
}
布尔isMatch(字符串s、字符串p){
如果(s==null | | p==null){
返回false;
}
//为什么要在字符串长度中添加额外的长度?
布尔值[][]dp=新布尔值[s.length()+1][p.length()+1];
dp[0][0]=真;
//这个检查的原因是什么?如果p在i=3时有'*',它就会通过
对于(int i=0;iQ:为什么要在字符串长度上添加额外的长度?
答:以获得更清晰的代码。

问:这项检查的原因是什么?如果p在i=3时有“”,它将通过*
答:它表示:如果模式中的第一个字符与字符串中的第一个字符匹配,则将尽可能多的字符串字符标记为匹配。这是贪婪的方法。

问:dp[i][j]不应该等于true吗?为什么要将布尔值设置为前面的字符?
答:没有。这是因为我们不想在之前的内容不匹配时指出字符匹配。简单地说,如果输入的3个字符不匹配,则后面的任何内容都不应标记为匹配。

问:不了解以下检查的目的和工作方式

答:如果字符与前一个不同,且前一个图案字符不是具有特殊意义的点,请保持该状态,因为我们可能有0个长度匹配。否则,只需在三个可能的方向中的一个方向上继续(向后、向上或向前看).

我很确定你是怎么迷路的,因为有些人说要解开这个谜团,然后根据语言条件和数组位置给出条件,也就是说,
如果p.charAt(j)==s.charAt(I):dp[I][j]=dp[I-1][j-1]
远离这些无知的东西。!!把时间花在一个目标上,最终的结果是一些代码。