找到最大的公共号码

时间:2017-03-13 04:27:25

标签: java arrays comparison

我正在尝试编写的代码假设接受一个2D整数数组,对每一行进行排序,然后找到行之间的最大公共数。我遇到了关于如何返回整数类型的问题。

目前我找不到一种方法来获取我创建的行数组并将其作为整数返回。同样,我知道测试总共有3行,但列数量未知。

import org.junit.Assert;

import org.junit.Test;



public class FindCommonTest {

@Test
public void testGetLargestCommonNumber1() {
    int[][] a = {{54, 41, 43, 55, 63}, {25, 40, 48, 12, 89}, {20, 19, 90, 94, 52}};
    Integer result = FindCommon.getLargestCommonNumber(a);
    Assert.assertNull(result);
}

@Test
public void testGetLargestCommonNumber2() {
    int[][] a = {{53, 41, 43, 55, 63}, {41, 25, 48, 12, 54}, {91, 19, 90, 54, 41}};
    Integer result = FindCommon.getLargestCommonNumber(a);
    Assert.assertEquals(41, (int) result);
}   

@Test
public void testGetLargestCommonNumber3() {
    int[][] a = {{54, 41, 43, 55, 63}, {25, 41, 48, 12, 54}, {41, 19, 90, 54, 94}};
    Integer result = FindCommon.getLargestCommonNumber(a);
    Assert.assertEquals(54, (int) result);
}   

}

我有用于解决此问题的测试代码。

var myObj={Chatting : {a :{ 'id' : 'a'}}};
alert(myObj.Chatting.a.id)

3 个答案:

答案 0 :(得分:0)

要返回返回的Integer类型,您只需执行此操作:

public static Integer getLargestCommonNumber(int[][] a) {
    //Check if array is empty
    if (a == null || a.length == 0) {
        return 0;
    }
    //Initialize
    int[] Row1 = a[0];
    int[] Row2 = a[1];
    int[] Row3 = a[2];

    Arrays.sort(a);

    int i = 0, j = 0, k = 0;
    Integer max = null;
    // Iterate through three arrays while all arrays have elements
    while (i < Row1.length && j < Row2.length && k < Row3.length) {
        if (Row1[i] > Row2[j] && Row2[j] > Row3[k]) {
            System.out.print(Row1[i] + " ");
            max = Row1[i];
        } else if (Row2[i] > Row1[j] && Row2[j] > Row3[k]) {
            System.out.print(Row2[i] + " ");
            max = Row2[i];
        } else if (Row3[i] > Row1[j] && Row3[j] > Row2[k]) {
            max = Row3[i];
        }
    }
    return max;
}

答案 1 :(得分:0)

while循环中的逻辑存在问题。目前,您并没有真正迭代这三个数组。你的终止条件听起来不错,但截至目前它将成为一个无限循环因为ijk没有改变。您应该增加包含要迭代的两个较小数字的两个数组的索引。此外,通过对每个数组使用相同的索引,确保一致。不要在不同阵列之间交换索引。例如,始终将iRow1一起使用,始终将jRow2一起使用,并始终将kRow3一起使用。此外,您没有对2d数组中的每一行进行排序。您需要在每一行上调用Arrays.sort()。最后,在while循环之外声明max。否则你将无法返回它,因为变量在循环中只有一个范围。

    //Check if array is empty
    if (a == null || a.length == 0){
        return 0;
    }
    for (int[] row : a) {
        Arrays.sort(row);
    }
    //Initialize
    int [] Row1 = a[0];
    int [] Row2 = a[1];
    int [] Row3 = a[2];
    int i = 0, j = 0, k = 0;

    int max = 0;
    // Iterate through three arrays while all arrays have elements
    while (i < Row1.length && j < Row2.length && k < Row3.length)
    {
         if (Row1[i] >= Row2[j] && Row1[i] >= Row3[k]){
             max = Row1[i];
             j++;
             k++;
         }
         else if (Row2[j] >= Row1[i] && Row2[j] >= Row3[k]){
             max = Row2[j];
             i++;
             k++;
         }
         else if (Row3[k] >= Row1[i] && Row3[k] >= Row2[j]){
             max = Row3[k];
             i++;
             j++;
         }
    }

现在,当此循环结束时,至少有一个数组已经完全遍历。因此,可能存在两行仍然需要从它们停止的位置迭代的情况。为了涵盖所有情况,您需要三个与上面类似的while循环来迭代其他两行中的其余部分。

while(i < Row1.length && j < Row2.length) {
    if(Row1[i] >= Row2[j]){
        max = Row1[i];
        j++;
    }
    else{
        max = Row2[j];
        i++;
    }
}
while(i < Row1.length && k < Row3.length) {
    if(Row1[i] > Row3[k]){
        max = Row1[i];
        k++;
    }
    else{
        max = Row3[k];
        i++;
    }
}
while(j < Row2.length && k < Row3.length) {
    if(Row2[j] >= Row3[k]){
        max = Row2[j];
        k++;
    }
    else{
        max = Row3[k];
        j++;
    }
}
return max;

最后,确保在方法结束时返回max

答案 2 :(得分:0)

此代码允许您拥有几乎任意数量的行。

public static Integer getLargestCommonNumber(int[][] a) {

    // Check if array is empty
    if (a == null || a.length == 0) {
        return 0;
    }
    // Initialize, we are going to create a List (matrix) of Lists (rows)
    List<List<Integer>> matrix = new ArrayList<>();
    for (int i = 0; i < a.length; i++) {
        int[] aRow = a[i];
        List<Integer> row = new ArrayList<>();
        for (int j = 0; j< aRow.length; j++) {
            row.add(aRow[j]);
        }
        matrix.add(row);

    }

    // sort the rows with largest number on the left
    for (List<Integer> row : matrix) {
        Collections.sort(row, new Comparator<Integer>(){
            @Override
            public int compare(Integer o1, Integer o2) {
                return -1 * o1.compareTo(o2);
            }});
    }

    // get just the first row
    List<Integer> row0 = matrix.get(0);
    // loop through each value in the row, from largest to smallest
    for (Integer row0Value : row0) {
        int count0 = 0; // this is a counter that just adds the row numbers
        int count1 = 0; // this is a counter that adds the row numbers of rows that have a match
        // loop through rows 2 to N
        for(int i = 1; i < matrix.size(); i++) {
            count0 += i; // add to the total row counter
            if (matrix.get(i).contains(row0Value)) {
                count1 += i; // add to counter only if there was a match in this row
            }
        }
        if (count0 == count1) { // if the value is in all rows then both counters should be the same
            return row0Value;
        }
    }
    // there were no values that matched in all of the rows
    return 0;
}