简体   繁体   中英

recursive method to how to count how many different numbers in array? [java]

I need to write a recursive function for counting how many different values in array, and the only values that can appear in it are 0-9 .

For example, for the following array:

{   { 1, 5, 4, 3 }, 
    { 4, 3, 2, 1 },     
    { 4, 5, 1, 4 },
    { 1, 4, 3, 2 }
};

The function will return 5 because the only values appearing in this array are: 1,2,3,4,5

That's what I have tried so far I don't understand how to promote the index without using for

public static int numOfColors(int[][] map) { 
    int i=0;
    int j=0;
    int colors=0;
    int contains=map[i][j];
    if (map == null ) { 
        return 0;
    } else if (map[i][j] != 0&&map[i][j]!=contains) { 
        colors ++;
    }
    return numOfColors(map) + 1;
}

If you have an array like: int numbers[ ][ ], you can do something like

public int getNUnique(int[][] numbers) {
     int usedNumbers[10];
     boolean found=false;
     for(int i=0, i<numbers.lenght, i++) {
         for(int j=0, i<numbers[].lenght, j++) {
              for(int x=0, x<usedNumbers.lenght, X++) {
                   if(numbers[i][j]==usedNumbers[x]) {
                       found=true;
                   }
                   if(!found) {
                       usedNumbers[usedNumbers.lenght]=numbers[i][j];
                   }
              }
         }
         return usedNumbers.length
     }

You need another parameter for the recursive call: that is a Set of Integers that you have already seen. Or you could use a BitSet, or even a string since you are restricted to single digits.

As for advancing the index, you could either pass in two parameters, representing row and column, or you could use a div(/) and mod (%) strategy to compute row and column from a single index parameter.

Finally, you might consider getting rid of the need for index altogether, by simply passing in an iterator instead of an array (although the idea of passing an iterator into a recursive call does seem odd):

public static int numColors(PrimitiveIterator.OfInt iter, BitSet seen) {
    if(!iter.hasNext())
        return seen.cardinality();
    seen.set(iter.nextInt());
    return numColors(iter, seen);
}
public static void main(String[] args) {
    int[][] arr = {
        { 1, 5, 4, 3 }, 
        { 4, 3, 2, 1 },     
        { 4, 5, 1, 4 },
        { 1, 4, 3, 2 }};
    PrimitiveIterator.OfInt iter  = Arrays.stream(arr).flatMapToInt(Arrays::stream).iterator();
    System.out.println(numColors(iter, new BitSet()));
}

And as requested, here is a more "stupid" approach:

public class SampleJava {
private static final int MAX_COLORS = 10;
public static int numColors(int[][] arr, int row, int column, int[] seen) {
    if(row == arr.length) // end of rows
        return 0; // done
    if(column == arr[row].length) // end of column
        return numColors(arr, row + 1, 0, seen); // advance to next row
    seen[arr[row][column]]++; // increment the "seen" count for this color 
    if(seen[arr[row][column]] == 1) // are we seeing a new color?
        return 1 + numColors(arr, row, column + 1, seen); // yes. next column
    return 0 + numColors(arr, row, column + 1, seen); // no. next column
}
public static void main(String[] args) {
    int[][] arr = {
        { 1, 5, 4, 3 }, 
        { 4, 3, 2, 1 },     
        { 4, 5, 1, 4 },
        { 1, 4, 3, 2 }};
    System.out.println(numColors(arr, 0, 0, new int[MAX_COLORS]));
}
}

正如其他人所建议的那样,您需要一个超出范围的元素来保持所访问的颜色(数字),该元素的生命周期与您使用的递归函数无关。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM