有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何确定一个数组是否包含一个单独数组中的所有整数

我在学校的ap计算机科学课上,我被这个问题困住了。甚至不能想出解决问题的办法

这里是逐字逐句: 编写一个名为contains的静态方法,该方法接受两个整数数组a1和a2作为参数,并返回一个布尔值,指示a2的元素序列是否出现在a1中(true表示是,false表示否)。a2中的元素序列可能出现在a1中的任何位置,但必须连续出现,且顺序相同。例如,如果名为list1和list2的变量存储以下值:

int[] list1 = {1, 6, 2, 1, 4, 1, 2, 1, 8};
int[] list2 = {1, 2, 1};

那么contains(list1, list2)的调用应该返回true,因为list2的值序列{1, 2, 1}包含在从索引5开始的list1中。如果list2存储了值{2, 1, 2},那么contains(list1, list2)的调用将返回false,因为list1不包含该值序列。具有相同元素的任何两个列表都被视为相互包含,因此contains(list1, list1)之类的调用应该返回true

您可以假设传递给方法的两个数组的长度至少为1。您不能使用任何字符串来帮助解决此问题,也不能使用生成字符串的方法,例如数组。toString

如果有人能给我指出正确的方向,那就太好了

这里还有一个我提出的尝试,但它没有足够的测试次数

public static boolean contains(int[] set1, int[] set2) {
    boolean contains = false;
    for (int i = 0; i < set1.length; i++) {
        for (int a = 0; a < set2.length - 1; a++) {
            if (set1[i] == set2[a] && set1[i + 1] == set2[a + 1]) {
                contains = true;
            } else {
                contains = false;
            }
        }
    }
    return contains;
}

共 (3) 个答案

  1. # 1 楼答案

    我想说的是,就心态而言,你应该认为“第一个元素与阵列对抗,直到匹配为止”

    public static boolean contains(int[] set1, int[] set2) {
        for (int i = 0; i < set1.length; i++) {
           int count = 0;
           for (int w = 0; w < set2.length; w++) {
              if (set2[w] == set1[i + w]) {
                  count++;
              } else {
                  count = 0;
                  continue;
              }
           }
           if (count == set2.length) {
               return true;
           }
        }
        return false;
    

    从这个意义上讲,您只需要在第二个数组中按需要进行比较。如果在遍历了set2中的所有元素后,得到的长度相同,那么它就包含在set1中。当然,如果你有问题,可以问:)

  2. # 2 楼答案

    Demo of this answer at IDEOne.com

    我想出了以下函数。阅读评论,了解其背后的逻辑:

    public static boolean contains(int[] a, int[] b) {
        //Loop until there aren't enough elements left in a to match b.
        for (int i = 0; i < a.length - b.length + 1; i++) {
            for (int j = 0; j < b.length; j++) {
    
                //If the jth element of b doesn't match
                //the corresponding element of a, then move
                //to the next step in the sequence.
                if (a[i + j] != b[j])
                    break;
    
                //If we are at the end of the loop, return
                //true because that means we found a consecutive match.
                if (j == b.length - 1)
                    return true;
    
            }
        }
    
        return false; //If we got here, there are no matches.
    }
    
  3. # 3 楼答案

    下面是一种递归方法:

    public static boolean contains(int[] set1, int[] set2) {
        //System.out.println(Arrays.toString(set1) + " " + Arrays.toString(set2));
    
        //set 2 cannot be contained within set 1 because there aren't 
        //enough elements. This either means that we recursed too deep
        //within the first set that there are not enough elements, or
        //there were not enough elements to begin with.
        if (set1.length < set2.length) return false;
    
        //from the start of each set, count the number of matches in order
        int numMatched = 0;
        while (numMatched < set2.length && set1[numMatched] == set2[numMatched]) {
            numMatched++;
        }
    
        if (numMatched == set2.length) 
            //the number of matches found equals the length of the set to
            //search for, so we have found a match. Return true to unravel
            //the recursion.
            return true;
        else {
            //we didn't find a match, so shift the array by 1 and then
            //recursively call this function to compare again.
            int[] subset = Arrays.copyOfRange(set1,  1,  set1.length);
            return contains(subset, set2);
        }
    
    }
    

    每次找不到匹配序列时,我们都会创建数组的一个子集,不包括第一个元素,并将其传递回contains以继续检查。以下是每个迭代的输出:

    第一次:set1= [1,6,2,1,4,1,2,1,8]和set2=[1,2,1] 在数组的开头没有找到匹配项(我们在比较6和2时中断。下一个递归调用是:

    set1= [6,2,1,4,1,2,1,8],[1,2,1]

    下一个递归比较[2,1,4,1,2,1,8][1,2,1]

    依此类推,直到最后的递归比较: [1,2,1,8][1,2,1]并按顺序查找匹配项