【问题标题】:How to check if two matrices have an identical row?如何检查两个矩阵是否具有相同的行?
【发布时间】:2018-02-01 13:06:16
【问题描述】:

我正在尝试用 Java 开发一种算法,如果给定两个矩阵(比如 ab),如果 ab 中至少有一行相同,则返回 true .

这是我尝试的方法:

public static boolean check_row(int a[][], int b[][]){
        boolean check = false;
        for(int i = 0; i < a.length; i++){
            for(int j = 0; j < b[0].length; j++){
                if(a[i][j] == b[i][j])
                    check = true;
            }
        }
        return check;
    }

这是一个简单的主要内容:

public static void main(String[] args){
        int a[][] = {{1,2}, {3,4}};
        int b[][] = {{1,2}, {7,8}};
        System.out.println(check_row(a, b));
    }

在这里我得到true,因为两个矩阵的第一行是相同的。 但是,如果我将矩阵初始化更改为:

int a[][] = {{1,2}, {3,4}};
int b[][] = {{5,6}, {1,2}};

我得到false,即使a 的第一行和b 的第二行相同。

我应该如何修改方法以便在这两种情况下都获得true

【问题讨论】:

    标签: java algorithm matrix


    【解决方案1】:

    你的条件太简单了......高级的想法是,从a和b中选择一行然后确定它是否相同,所以你需要3个循环......

    代码:

    public class SameRowFinder {
    
        public static void main(String[] args){
            int a[][] = {{1,2},{3,4}};
            int b[][] = {{1,2}, {7,8}};
            System.out.println(hasSameRow(a, b));
    
            int aa[][] = {{1,2},{3,4}};
            int bb[][] = {{5,6}, {1,2}};
            System.out.println(hasSameRow(aa, bb));
        }
    
        private static boolean hasSameRow(int[][] a, int[][] b) {
            for (int i = 0; i < a.length; i++) {
                for (int j = 0; j < b.length; j++) {
                    if (isSameRow(a[i], b[j])) {
                        System.out.printf("Same rows are %d and %d (0-based).%n", i, j);
                        return true;
                    }
                }
            }
            return false;
        }
    
        private static boolean isSameRow(int[] row1, int[] row2) {
            if (row1.length != row2.length) {
                throw new IllegalArgumentException("rows with different length");
            }
            for (int i = 0; i < row2.length; i++) {
                if (row1[i] != row2[i]) {
                    return false;
                }
            }
            return true;
        }
    }
    

    另外,您不需要编写自己的数组比较函数,而是使用Arrays.equal(int[], int[]),但它只会隐藏第三个循环。如果数组长度不同,上述方法会引发运行时异常。绝对值得看看 Arrays.equal(int[], int[]) 的实现以获取一些提示(检查相等性 + 空值检查)。

    【讨论】:

      【解决方案2】:

      您的第二个循环中有错误。 变化:

      for(int j = 0; j < b[0].length; j++)
      

      for(int j = 0; j < b.length; j++){
      

      此外,问题在于您无法像以前那样比较行。 您必须检查行的长度是否相同,最后比较行。您将需要一个额外的 for 循环来比较行。

      这个可以解决问题:

      public static boolean check_row(int a[][], int b[][]){
          boolean check = false;
          for(int i = 0; i < a.length; i++){
              for(int j = 0; j < b.length; j++){
                  if (compareRows(a[i], b[j]))
                      check = true;
              }
          }
          return check;
      }
      
      private static boolean compareRows(int[] row1, int[] row2) {
          if (row1.length == row2.length) {
              for (int i = 0; i < row2.length; i++) {
                  if (row1[i] != row2[i]) {
                      return false;
                  }
              }
              return true;
          }
          return false;
      }
      
      public static void main(String[] args){
          int a[][] = {{1,2},{3,4}};
          int b[][] = {{5,6}, {1,2}};
          System.out.println(check_row(a, b));
      }
      

      【讨论】:

      • 没试过,只看到第二个for循环的错误。我使用有效的解决方案编辑响应。
      【解决方案3】:

      在 OP 提供的示例中,b 矩阵中的行循环丢失。

      学习时,算法可以写成如下:

      public static boolean check_row(int a[][], int b[][]) {
          int row_size = a[0].length;
          for (int i = 0; i < a.length; i++) {
              b: for (int j = 0; j < b.length; j++) {
                  for (int k = 0; k < row_size; k++) {
                      if (a[i][k] != b[j][k])
                          continue b; // move to next row in 'b' matrix
                  }
                  return true; // all elements in row were equal if we reached this point
              }
          }
          return false;
      }
      

      在现实生活中可能是这样的:

      public static boolean check_row(int[][] a, int[][] b) {
          return Arrays.stream(b)
                       .anyMatch(rowB -> Arrays.stream(a).anyMatch(rowA -> Arrays.equals(rowA, rowB)));
      }
      

      【讨论】:

        【解决方案4】:

        希望这段代码可以帮到你

        public static boolean check_row(int a[][], int b[][])
        {
            boolean check = false;
        
            for(int i = 0; i < a.length; i++)
            {
        
                for(int k = 0 ; k< b.length ; k++)
                {
        
                    for(int j = 0 ; j<b[0].length ; j++)
                    {
                        if(a[i][j]!=b[k][j])
                        {
                            break;
                        }//if even one cell were not similar in both a and b break
                        else
                        {
                            if(j==b[0].length-1)
                            {
                                check = true;
                            }//if you haven't broken the loop yet and if j went till the end
                        }//else if they are equal
        
                    }//j to go through columns
                    if(check == true)
                    {
                       break;
                    }//for bringing down the time complexity
                }//k to go through b rows
                if(check == true)
                {
                   break;
                }//for bringing down the time complexity
            }//i
            return check;
        }
        

        【讨论】:

          猜你喜欢
          • 2012-04-11
          • 2016-04-08
          • 1970-01-01
          • 2021-12-22
          • 2022-01-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-04
          相关资源
          最近更新 更多