错误:类型不兼容:void 无法转换为 int[][] [in __Driver__.java] [关闭]

问题描述 投票:0回答:1
public class FloodFillAlgo {

    static int m = 3;
    static int n = 3;
    public static void helper(int image[][], int sr, int sc, int color, int orgCol) {
        if(sr < 0 || sc < 0 || sr >= image.length || sc >= image[0].length || 
        image[sr][sc] != orgCol) {
           return;                **//I am getting this error here in this line.**
        }
        image[sr][sc] = color; 
        //left
        helper(image, sr, sc-1, color, orgCol);
        //right
        helper(image, sr, sc+1, color, orgCol);
        //up
        helper(image, sr-1, sc, color, orgCol);
        //down
        helper(image, sr+1, sc, color, orgCol);
    }
    
    public static void floodFill(int image[][], int sr, int sc, int color) {
        if(image[sr][sc] == color) {
            return;
        }
        helper(image, sr, sc, color, image[sr][sc]);
        return;
    }
    public static void main(String args[]) {
        int image[][] = {{1, 1, 1},
                        {1, 1, 0},
                        {1, 0, 1}};
        floodFill(image, 1, 1, 2);
        for(int i=0; i<m; i++) {
            for(int j=0; j<n; j++) {
                System.out.print(image[i][j]+" ");
            }
            System.out.println();
        }
    }
}

当我在 LeetCode 中编写这段代码时,它显示了我上面提到的错误。 当我在我的 Vs Code 上运行这段代码时,它工作正常。

java
1个回答
0
投票

更仔细地阅读 LeetCode 问题...它是否说写一个 returns

int[][]
的 helper(或 floodFill)函数?

如果是这样,那是 LeetCode 的编译器告诉你你的函数当前被定义为返回一个 void 结果,这不是预期的

更具体地说,LeetCode 中包含的单元测试需要访问函数返回结果,因此它可以检查您的代码是否正确。它不能用 void 函数做到这一点

© www.soinside.com 2019 - 2024. All rights reserved.