Java 数组中的不同值

Java 数组中的不同值,java,algorithm,recursion,Java,Algorithm,Recursion,我正在使用Java,我正在尝试只使用递归函数而不使用HashSet ArrayList等从2d数组获取所有不同的值。。, 值将仅为[0-9] i、 e: 我尝试的是: public static int numOfColors(int[][] map) { int colors = 0; if (map == null || map.length == 0) { return colors; } else { int[] subArr =

我正在使用Java,我正在尝试只使用递归函数而不使用HashSet ArrayList等从2d数组获取所有不同的值。。, 值将仅为[0-9] i、 e:

我尝试的是:

public static int numOfColors(int[][] map) {
    int colors = 0;
    if (map == null || map.length == 0) {
        return colors;
    } else {
        int[] subArr = map[map.length - 1];

        for (int i = 0; i < subArr.length; i++) {
            int j = i + 1;
            for (; j < subArr.length; j++) {
                if (subArr[i] == subArr[j]) {
                    break;
                }
            }
            if (j == subArr.length) {
                int k = 0;
                for (; k < map.length - 1; k++) {
                    for (int l = 0; l < map[k].length; l++) {
                        if (subArr[i] == map[k][l]) {
                            continue;
                        }
                    }
                }
                if (k == map.length - 1) {
                    colors++;
                }
            }
        }
        int[][] dest = new int[map.length - 1][];
        System.arraycopy(map, 0, dest, 0, map.length - 1);
        colors += numOfColors(dest);

        return colors;
    }
}
publicstaticintnumofolors(int[]map){
int colors=0;
if(map==null | | map.length==0){
返回颜色;
}否则{
int[]subar=map[map.length-1];
对于(int i=0;i

但这对我不起作用,miskate在哪里?

我知道这不是答案,但你应该始终思考你的解决方案是否合理

在我看来,在这里使用递归是一个非常糟糕的主意,因为:

  • 代码根本不可读
  • 我还没有检查过,但我怀疑它是否更有效
  • 递归很难调试
  • 你把简单的问题复杂化了
考虑以下代码。它正是您所需要的:

Integer[][] array = {{4,2,2,1,4},{4,4,3,1,4},{1,1,4,2,1},{1,4,0,2,2},{4,1,4,1,1}};
int size = Arrays.stream(array)
        .flatMap(Arrays::stream)
        .collect(Collectors.toSet())
        .size();
System.out.println("size = " + size);

如果您有意在这种情况下使用递归,那么我唯一可以推荐的就是测试驱动开发。同时编写算法和测试。

正如@Cash Lo已经提到的,您需要某种存储。因此,您的算法可能看起来像:

@Test
public void numOfColorsTest() {
    int[][] map = new int[][] {{4,2,2,1,4},{4,4,3,1,4},{1,1,4,2,1},{1,4,0,2,2},{4,1,4,1,1}};
    System.out.println(String.format("numOfColors: %s", numOfColors(map, new int[0], map.length-1)));

    map = new int[][] {{4,6,2,1,4},{4,4,3,1,4},{1,1,4,2,1},{1,4,0,2,2},{4,1,4,1,1}};
    System.out.println(String.format("numOfColors: %s", numOfColors(map, new int[0], map.length-1)));

    map = new int[][] {{4,4,4,4,4}};
    System.out.println(String.format("numOfColors: %s", numOfColors(map, new int[0], map.length-1)));
}

public static int numOfColors(int[][] map, int[] collector, int currentPosition) {
    int[] result = collector;
    if (currentPosition < 0) {
        return collector.length;
    }
    for (int color : map[currentPosition]) {
        boolean found = false;
        for (int aResult : result) {
            if (aResult == color) {
                found = true;
                break;
            }
        }
        if (!found) {
            int[] newResult = new int[result.length + 1];
            System.arraycopy(result, 0, newResult, 0, result.length);
            newResult[newResult.length - 1] = color;
            result = newResult;
        }
    }
    return numOfColors(map, result, currentPosition-1);
}
@测试
公共无效numOfColorsTest(){
int[][]map=新的int[][{{4,2,2,1,4},{4,4,3,1,4},{1,1,4,2,1},{1,4,0,2,2},{4,1,4,1,1};
System.out.println(String.format(“numofolors:%s”,numofolors(map,newint[0],map.length-1));
map=newint[][{4,6,2,1,4},{4,4,3,1,4},{1,1,4,2,1},{1,4,0,2,2},{4,1,4,1,1};
System.out.println(String.format(“numofolors:%s”,numofolors(map,newint[0],map.length-1));
map=newint[][{{4,4,4,4}};
System.out.println(String.format(“numofolors:%s”,numofolors(map,newint[0],map.length-1));
}
公共静态int numofolors(int[]map,int[]collector,int currentPosition){
int[]结果=收集器;
如果(当前位置<0){
返回收集器长度;
}
用于(int-color:map[currentPosition]){
布尔值=false;
for(int-aResult:result){
if(aResult==颜色){
发现=真;
打破
}
}
如果(!找到){
int[]newResult=newint[result.length+1];
数组复制(result,0,newResult,0,result.length);
newResult[newResult.length-1]=颜色;
结果=新结果;
}
}
返回numOfColors(映射、结果、当前位置-1);
}

递归在这里没有多大意义。只需使用一个简单的数组作为存储,并计算不同值的实例,如果您知道范围(0-9),那么一个简单的int[]就足够了

这应该可以做到:

public static int numOfColors(int[][] map){

    int[] storage = new int[10];

    //iterate through all the values
    for(int i = 0; i<map.length; i++){
        for(int j = 0; j<map[0].length; j++){

            //will throw an Exception if an entry in map is not 0-9
            //you might want to check for that
            storage[map[i][j]]++;

        }
    }

    int colors = 0;
    //now check which values exist.
    for(int i = 0; i<storage.length; i++){
        if(storage[i] != 0) colors++;
    }

    return colors;
}
publicstaticintnumofolors(int[]map){
int[]存储=新int[10];
//遍历所有值

对于(int i=0;我为什么使用递归?你需要某种存储来知道你以前是否看到过值,或者在计算值之后删除值的所有瞬间,因此汇总每个子数组的颜色是不可行的。此外,递归在这里似乎是一个奇怪的选择,因为它既不能简化问题,也不能使解决方案更有效调试帮助(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题说明的问题对其他读者没有用处。请参阅:如何创建注释:值将仅为{0,1,2,3,4,5,6,7,8,9}
public static int numOfColors(int[][] map){

    int[] storage = new int[10];

    //iterate through all the values
    for(int i = 0; i<map.length; i++){
        for(int j = 0; j<map[0].length; j++){

            //will throw an Exception if an entry in map is not 0-9
            //you might want to check for that
            storage[map[i][j]]++;

        }
    }

    int colors = 0;
    //now check which values exist.
    for(int i = 0; i<storage.length; i++){
        if(storage[i] != 0) colors++;
    }

    return colors;
}