【问题标题】:Check if one array is a subset of the other array - special case检查一个数组是否是另一个数组的子集 - 特殊情况
【发布时间】:2021-07-10 04:14:28
【问题描述】:

我正在处理一个初学者问题,该问题检查一个数组是否是另一个数组的子集,但我遇到了一个特殊情况,下面是案例示例:a = [1,2,3]b = [1,1],其中@987654323 @ 包含1b 仅包含1,但b 不是a 的子集,因为b 包含1s 中的两个。

如何修改我的代码以使其检查这种特殊情况?

下面是sn-p的代码:

// True if one array is the subset of another array
public boolean checkSubset(int[] arr1, int[] arr2) {
    // A counter to remember how many
    // times the same element is found
    int cnt = 0;

    // If one of the array is empty, for empty set
    if (arr1.length == 0 || arr2.length == 0) {
        return true;
    }

    // Compare elements in two arrays
    for (int i = 0; i < arr1.length; ++i) {
        for (int j = 0; i < arr2.length; ++j) {
            if (arr1[i] == arr2[j]) {
                ++cnt;
                break;
            }
        }
    }

    // cnt would equal to the length of arr1 or arr2
    // if one array is the subset of the other one
    return (cnt == arr1.length || cnt == arr2.length);
}

【问题讨论】:

    标签: java arrays subset


    【解决方案1】:

    您可能需要另一个array c 来收集array b 的所有唯一值,然后比较array aarray c

    【讨论】:

      【解决方案2】:

      您可以为两个列表实现自定义containsAll方法,而不是数组

      /**
       * @param a   first list.
       * @param b   second list.
       * @param <T> type of list elements.
       * @return whether the first list contains all the elements from the second list.
       */
      public static <T> boolean containsAll(List<T> a, List<T> b) {
          // incorrect incoming data
          if (a == null || b == null) return false;
          // copy of the first list
          List<T> c = new ArrayList<>(a);
          // iterate through the second list and remove its
          // elements from the copy of the first list, if any
          for (T e : b) c.remove(e);
          // return whether the size of the first list
          // is equal to the size of the second list plus
          // what is left in the copy of the first list
          return a.size() == b.size() + c.size();
      }
      
      public static void main(String[] args) {
          List<Integer> a = Arrays.asList(1, 2, 3);
          List<Integer> b = Arrays.asList(1, 1);
          boolean isSubset = containsAll(a, b);
          System.out.println(isSubset); // false
      }
      

      另见:What is the quickest way to determine if two elements are in the same row or column in a 2D array?

      【讨论】:

        【解决方案3】:

        您可以为两个数组实现自定义泛型containsAll 方法,如下所示:

        /**
         * @param a   first array.
         * @param b   second array.
         * @param <T> type of array elements.
         * @return whether the first array contains
         * all the elements from the second array.
         */
        public static <T> boolean containsAll(T[] a, T[] b) {
            // incorrect incoming data
            if (a == null || b == null) return false;
            // copy of the first array
            T[] c = Arrays.copyOf(a, a.length);
            // iterate through the second array and remove its
            // elements from the copy of the first array, if any
            for (T e : b)
                // iterate through the copy of the first array
                for (int i = 0; i < c.length; i++)
                    // this element is equal to the
                    // element from the second array
                    if (c[i] != null && c[i].equals(e)) {
                        // shift the subsequent elements back
                        System.arraycopy(c, i + 1, c, i, c.length - i - 1);
                        // truncate the array by one element
                        c = Arrays.copyOf(c, c.length - 1);
                        // exit the inner loop
                        break;
                    }
            // return whether the size of the first array
            // is equal to the size of the second array plus
            // what is left in the copy of the first array
            return a.length == b.length + c.length;
        }
        
        public static void main(String[] args) {
            Integer[] a = {1, 2, 3, 1};
            Integer[] b = {1, 1};
            boolean isSubset = containsAll(a, b);
            System.out.println(isSubset); // true
        }
        

        【讨论】:

          猜你喜欢
          • 2010-09-24
          • 2012-05-20
          • 1970-01-01
          • 1970-01-01
          • 2016-12-13
          • 2020-03-10
          • 2012-08-29
          • 1970-01-01
          相关资源
          最近更新 更多