检查一维数组是否是另一个二维数组的每一行的子集

假设有一个一维数组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");

            }

        }

    }

}

但我得到的输出是:

http://img4.mukewang.com/62c54eb40001bfc503060172.jpg

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

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


小唯快跑啊
浏览 186回答 2
2回答

12345678_0001

你搞砸了循环的顺序:你应该先迭代arr。这个想法是 ti 使用 flags: isSubset,当在一行中找不到某个元素时,它变为 false,contains如果当前检查的元素在一行中,则变为 true。可以进行一些改进(如 foreach 循环和标记中断),但我决定保持代码简单。public class GFG {&nbsp; &nbsp; public static void main(String args[]) {&nbsp; &nbsp; &nbsp; &nbsp; int arr[][] = { { 11, 1, 13, 3, 7 },&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ 11, 1, 17, 7, 3 },&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ 2, 5, 8, 9, 10 } };&nbsp; &nbsp; &nbsp; &nbsp; int test[] = { 11, 3, 7, 1 };&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < arr.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boolean isSubset = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < test.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boolean contains = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int k = 0; k < arr[i].length; k++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (test[j] == arr[i][k]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contains = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!contains) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isSubset = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isSubset) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("test[] is " + "subset of arr " + i + " row");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("test[] is " + "not a subset of arr " + i + " row");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

慕运维8079593

尝试将您的解决方案分解为更小的方法,这样您的代码会更清晰,您可以更容易地想到发生了什么。这是一个如何完成的示例:class GFG {&nbsp; public static void main(String args[]) {&nbsp; &nbsp; int arr1[][] = { { 11, 1, 13, 3, 7 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 11, 1, 17, 7, 3 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 2, 5, 8, 9, 10 } };&nbsp; &nbsp; int test[] = { 11, 3, 7, 1 };&nbsp; &nbsp; System.out.println(arr1[0].length); // just for testing if it works&nbsp; &nbsp; for (int i = 0; i < arr1.length; i++) {&nbsp; &nbsp; &nbsp; if (isSubset(test, arr1[i])) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("test[] is " + "subset of arr1 " + i + " row");&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("test[] is " + "not a subset of arr1 " + i + " row");&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }&nbsp; /* returns true if arr contains all elements of sub */&nbsp; static boolean isSubset(int[] sub, int[] arr) {&nbsp; &nbsp; for (int e : sub) {&nbsp; &nbsp; &nbsp; if (!contains(e, arr)) return false;&nbsp; &nbsp; }&nbsp; &nbsp; return true;&nbsp; }&nbsp; /* returns true if arr contains elem */&nbsp; static boolean contains(int elem, int[] arr) {&nbsp; &nbsp; for (int e : arr) {&nbsp; &nbsp; &nbsp; if (elem == e) return true;&nbsp; &nbsp; }&nbsp; &nbsp; return false;&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java