亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

檢查一維數組是否是另一個二維數組的每一行的子集

檢查一維數組是否是另一個二維數組的每一行的子集

小唯快跑啊 2022-07-06 16:58:01
假設有一個一維數組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 arr1test is the subset of row 1 of arr1test 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");            }        }    }}但我得到的輸出是:我意識到這是 i 循環重復打印它,但在這種情況下我仍然沒有得到令人滿意的輸出。在這里可以做什么?或者這個問題有很多優化的實現嗎?歡迎任何建議。
查看完整描述

2 回答

?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

你搞砸了循環的順序:你應該先迭代arr。這個想法是 ti 使用 flags: 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");

            }

        }

    }

}


查看完整回答
反對 回復 2022-07-06
?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

嘗試將您的解決方案分解為更小的方法,這樣您的代碼會更清晰,您可以更容易地想到發生了什么。這是一個如何完成的示例:


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;

  }

}


查看完整回答
反對 回復 2022-07-06
  • 2 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號