【问题标题】:Check if an 1 dimensional array is a subset of each row of another 2 dimensional array检查一维数组是否是另一个二维数组的每一行的子集
【发布时间】:2019-02-23 06:41:53
【问题描述】:

假设有一个一维数组test[]={1,2,3} 和一个二维数组arr1[3][5]={{1,2,5,4,3},{3,7,1,4,2},{2,9,7,8,3}}

输出所需的内容如下:

test is the subset of row 0 of arr1
test is the subset of row 1 of arr1
test is not the subset of row 2 of arr1

这是我目前实现的代码:

class GFG {
    public static void main(String args[]) {
        int arr1[][] = { { 11, 1, 13, 3, 7 }, 
                         { 11, 1, 17, 7, 3 }, 
                         { 2, 5, 8, 9, 10 } };

        int test[] = { 11, 3, 7, 1 };

        int m = arr1.length; // rows
        int n = test.length;
        int o = arr1[0].length; // no. of elements in each row

        System.out.println(o); // just for testing if it works

        int i = 0;
        int j = 0;
        int k = 0;

        for (i = 0; i < n; i++) {
            for (j = 0; j < m && j != m; j++) {
                for (k = 0; k < o; k++)
                    if (test[i] == arr1[j][k])
                        break;

                if (k == o)
                    System.out.println("test[] is " + "not a subset of arr1 " + j + " row");
                else
                    System.out.println("test[] is " + "subset of arr1 " + j + " row");
            }
        }
    }
}

但我得到的输出是:

output here

我意识到这是 i 循环反复打印它,但在这种情况下我仍然没有得到令人满意的输出。

这里可以做什么? 或者这个问题有很多优化的实现吗? 欢迎提出任何建议。

【问题讨论】:

标签: java arrays


【解决方案1】:

您弄乱了循环顺序:您应该首先迭代 arr。这个想法是使用标志:isSubset,当在一行中没有找到某个元素时变为 false,contains,如果当前检查的元素在一行中,则变为 true。

可以进行一些改进(例如 foreach 循环和标记的中断),但我决定保持代码简单。

public class GFG {
    public static void main(String args[]) {
        int arr[][] = { { 11, 1, 13, 3, 7 }, 
                         { 11, 1, 17, 7, 3 }, 
                         { 2, 5, 8, 9, 10 } };

        int test[] = { 11, 3, 7, 1 };


        for (int i = 0; i < arr.length; i++) {
            boolean isSubset = true;
            for (int j = 0; j < test.length; j++) {
                boolean contains = false;
                for (int k = 0; k < arr[i].length; k++) {
                    if (test[j] == arr[i][k]) {
                        contains = true;
                        break;
                    }
                }
                if (!contains) {
                    isSubset = false;
                    break;
                }
            }
            if (isSubset) {
                System.out.println("test[] is " + "subset of arr " + i + " row");
            } else {
                System.out.println("test[] is " + "not a subset of arr " + i + " row");
            }
        }
    }
}

【讨论】:

  • 即使我认为我搞砸了循环。谢谢!
【解决方案2】:

尝试将您的解决方案分解为更小的方法,这样您的代码会更清晰,您可以更轻松地想到发生了什么。这是一个如何完成的示例:

class GFG {
  public static void main(String args[]) {
    int arr1[][] = { { 11, 1, 13, 3, 7 },
          { 11, 1, 17, 7, 3 },
          { 2, 5, 8, 9, 10 } };
    int test[] = { 11, 3, 7, 1 };

    System.out.println(arr1[0].length); // just for testing if it works

    for (int i = 0; i < arr1.length; i++) {
      if (isSubset(test, arr1[i])) {
        System.out.println("test[] is " + "subset of arr1 " + i + " row");
      } else {
        System.out.println("test[] is " + "not a subset of arr1 " + i + " row");
      }
    }
  }

  /* returns true if arr contains all elements of sub */
  static boolean isSubset(int[] sub, int[] arr) {
    for (int e : sub) {
      if (!contains(e, arr)) return false;
    }
    return true;
  }

  /* returns true if arr contains elem */
  static boolean contains(int elem, int[] arr) {
    for (int e : arr) {
      if (elem == e) return true;
    }
    return false;
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-12
    • 1970-01-01
    • 2010-09-24
    • 2021-08-31
    • 2012-05-20
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    相关资源
    最近更新 更多