【问题标题】:What is the quickest way to determine if two elements are in the same row or column in a 2D array?确定两个元素是否在二维数组中的同一行或同一列中的最快方法是什么?
【发布时间】:2021-07-09 18:32:54
【问题描述】:

作为我的一个程序的一部分,我正在尝试检查二维数组中的两个元素是否属于同一行或同一列。我知道可能有很多方法可以解决这个问题,主要是循环和遍历数组,但我只是想知道:有没有什么快速的方法来确定这一点?

假设我有一个看起来像{{1, 2}, {3, 4}, {5, 6}} 的二维数组。是否有任何快速的方法来确定12 属于同一行?几乎可以在 if 语句中将其评估为 “如果项目 x 与项目 y 属于同一行,那么就这样做”?如果没有,那么最快的方法是什么?

【问题讨论】:

    标签: java arrays loops multidimensional-array data-structures


    【解决方案1】:

    这是行的一种方法。

    int[][] v = {{1, 2}, {3, 4}, {5, 6}};
    System.out.println(rowContains(v, 2, 3));
    System.out.println(rowContains(v, 5, 6));
    

    打印

    false
    true
    

    方法

    • 流式传输单个 d 数组并忽略重复项
    • 在查找值时计数过滤器。
    • 如果任何最终计数为 2,则返回 true。
    public static boolean rowContains(int[][] v, int v1, int v2) {
        return Arrays.stream(v)
                .map(arrs -> Arrays.stream(arrs)
                        .distinct().filter(a -> a == v1 || a == v2)
                        .count()).anyMatch(a -> a == 2);
    }
    

    对于列,最简单的方法是编写一个方法来转置列和行并重新运行该方法。

    【讨论】:

      【解决方案2】:

      使用 rows 比使用 columns 更容易解决此任务。您可以使用binarySearch 方法。

      此解决方案也适用于参差不齐的二维数组和无限数量的要搜索的元素。

      public static void main(String[] args) {
          int[][] arr = {{1, 2, 3}, {4, 5}, {6, 7, 8}};
          System.out.println(sameRow(arr, 4, 5)); // true
          System.out.println(sameCol(arr, 3, 8)); // true
      }
      
      /**
       * @param arr      2d array.
       * @param elements 1d array of elements to search for.
       * @return whether any column of a 2d array contains all elements from a 1d array.
       */
      public static boolean sameCol(int[][] arr, int... elements) {
          return IntStream
                  // iterate through the columns
                  .iterate(0, i -> i + 1)
                  // take an array of values from the column, if any
                  .mapToObj(i -> Arrays
                          // iterate through the rows
                          .stream(arr)
                          // take those rows where this column is present
                          .filter(row -> row.length > i)
                          // take the value from the column
                          .mapToInt(row -> row[i])
                          // int[] - column
                          .toArray())
                  // until the columns are still present
                  .takeWhile(col -> col.length > 0)
                  // whether any column contains all the search elements
                  .anyMatch(col -> containsAll(col, elements));
      }
      
      /**
       * @param arr      2d array.
       * @param elements 1d array of elements to search for.
       * @return whether any row of a 2d array contains all elements from a 1d array.
       */
      public static boolean sameRow(int[][] arr, int... elements) {
          return Arrays.stream(arr).anyMatch(row -> containsAll(row, elements));
      }
      
      /**
       * @param a first array.
       * @param b second array.
       * @return whether the first array contains all elements from the second array.
       */
      public static boolean containsAll(int[] a, int[] b) {
          return Arrays.stream(b).allMatch(i -> Arrays.binarySearch(a, i) > -1);
      }
      

      另见:
      Filling a jagged 2d array first by columns
      Check if one array is a subset of the other array - special case

      【讨论】:

        【解决方案3】:

        此代码在没有 Java 7 中工作。该方法与使用binarySearch 方法相同。接受参差不齐的二维数组和无限数量的元素进行搜索。

        还有loops,这个任务用rows比用columns更容易解决。

        public static void main(String[] args) {
            int[][] arr = {{1, 2, 3}, {4, 5}, {6, 7, 8}};
            System.out.println(sameRow(arr, 4, 5));    // true
            System.out.println(sameCol(arr, 3, 8));    // true
            System.out.println(sameCol(arr, 2, 5, 7)); // true
        }
        
        /**
         * @param arr      2d array.
         * @param elements 1d array of elements to search for.
         * @return whether any column of a 2d array contains all elements from a 1d array.
         */
        public static boolean sameCol(int[][] arr, int... elements) {
            // iterate through the columns
            for (int i = 0; ; i++) {
                // take an array of values from the column, if any
                int[] col = new int[arr.length];
                // column counter
                int j = 0;
                // iterate through the rows
                for (int[] row : arr)
                    // take those rows where this column is present
                    if (row.length > i)
                        // take the value from the column
                        // and increase the counter
                        col[j++] = row[i];
                // until the columns are still present
                if (j == 0) break;
                // if this column contains all the search elements
                if (containsAll(Arrays.copyOf(col, j), elements))
                    return true;
            }
            return false;
        }
        
        /**
         * @param arr      2d array.
         * @param elements 1d array of elements to search for.
         * @return whether any row of a 2d array contains all elements from a 1d array.
         */
        public static boolean sameRow(int[][] arr, int... elements) {
            for (int[] row : arr)
                if (containsAll(row, elements))
                    return true;
            return false;
        }
        
        /**
         * @param a first array.
         * @param b second array.
         * @return whether the first array contains all elements from the second array.
         */
        public static boolean containsAll(int[] a, int[] b) {
            for (int i : b)
                if (Arrays.binarySearch(a, i) < 0)
                    return false;
            return true;
        }
        

        【讨论】:

          猜你喜欢
          • 2021-11-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-03-11
          • 1970-01-01
          • 1970-01-01
          • 2015-02-07
          • 1970-01-01
          相关资源
          最近更新 更多