本文介绍了我如何确定一个数组包含在一个单独的阵列中的所有整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的学校的AP计算机科学类和我被困在这一个问题。并不能真正竟然真的拿出如何解决它的想法。

这是一字:
编写了一个名为包含接受整数A1和A2作为参数和返回指示元素A2的序列,无论是否出现在A1中的布尔值的两个阵列(真实的静态方法是的,假的无)。在A2的元素的序列可以a1中的任何地方出现,但必须出现连续并以相同的顺序。例如,如果变量称为list1的和list2中存储下列值:

  INT [] list1的= {1,6,2,1,4,1,2,1,8};
INT []列表2 = {1,2,1};

然后的调用包含(list1的,列表2)应返回true,因为值列表2的序列 {1,2,1} 包含List1中从索引5.如果列表2已经存储 {2,1,2} ,包含值(list1的,列表2)将返回false,因为list1的不包含值的序列。具有相同元素的任何两个列表都被认为互相牵制,所以一个电话,如包含(list1的,list1的)应返回true。

您可以假设传递给你的方法两个数组将产生的字符串,如Arrays.toString至少为1的长度,不得使用任何字符串来帮你解决这个问题,也没有方法。

如果有人可以点我这将是伟大正确的方向。

还这里有一个尝试,我想到了,但它没有足够数量的测试

 公共静态布尔为contains(int [] SET1,INT []设定2){
    布尔包含= FALSE;
    的for(int i = 0; I< set1.length;我++){
        对于(int类型的= 0; A< set2.length - 1; ++){
            如果(SET1 [I] == SET2 [A]和放大器;&安培; SET1 [I + 1] ==设置2 [A + 1]){
                包含= TRUE;
            }其他{
                包含= FALSE;
            }
        }
    }
    返回包含;
}


解决方案

连续

 公共静态布尔为contains(int [] SET1,INT []设定2){
     外:
     的for(int i = 0; I< set1.length - set2.length;我++){
         对于(INT J = 0; J< set2.length; J ++){
             如果(SET1 [I + J]!= SET2 [J]。)
                  继续OUTER;
         }
         返回true;
     }
     返回false;
}


要避免标签可以使用这可能是更清晰的方法

 公共静态布尔为contains(int [] SET1,INT []设定2){
     的for(int i = 0; I< set1.length - set2.length;我++)
         如果(!比赛(集1,我,设置2))
             返回false;
     返回true;
}公共静态布尔匹配(INT [] SET1,诠释过,INT []设定2){
     对于(INT J = 0; J< set2.length; J ++)
         如果(SET1 [关+ J]!= SET2 [J]。)
               返回false;
     返回true;
}


如果它仅需要是为了

 公共静态布尔为contains(int [] SET1,INT []设定2){
     的for(int i = 0,J = 0; I< set1.length;我++)
         如果(SET1 [I] == SET2 [J]。)
             如果(++ J> = set2.length)
                 返回true;
     返回false;
}

I'm in my schools ap computer science class and I'm stuck on this one problem. and cant really even really come up with an idea on how to solve it.

Here it is word for word:Write a static method named contains that accepts two arrays of integers a1 and a2 as parameters and that returns a boolean value indicating whether or not a2's sequence of elements appears in a1 (true for yes, false for no). The sequence of elements in a2 may appear anywhere in a1 but must appear consecutively and in the same order. For example, if variables called list1 and list2 store the following values:

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

Then the call of contains(list1, list2) should return true because list2's sequence of values {1, 2, 1} is contained in list1 starting at index 5. If list2 had stored the values {2, 1, 2}, the call of contains(list1, list2) would return false because list1 does not contain that sequence of values. Any two lists with identical elements are considered to contain each other, so a call such as contains(list1, list1) should return true.

You may assume that both arrays passed to your method will have lengths of at least 1. You may not use any Strings to help you solve this problem, nor methods that produce Strings such as Arrays.toString.

If someone could point me in the right direction that would be great.

also here's one attempt i came up with but it doesn't have a sufficient number of tests

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;
}
解决方案

For consecutive

public static boolean contains(int[] set1, int[] set2) {
     OUTER:
     for (int i = 0; i < set1.length - set2.length; i++) {
         for (int j = 0; j < set2.length; j++) {
             if (set1[i + j] != set2[j])
                  continue OUTER;
         }
         return true;
     } 
     return false;
}


To avoid a label you can use a method which might be clearer

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

public static boolean matches(int[] set1, int off, int[] set2) {
     for (int j = 0; j < set2.length; j++)
         if (set1[off + j] != set2[j])
               return false;
     return true;
}


If it only needs to be in order

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

这篇关于我如何确定一个数组包含在一个单独的阵列中的所有整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 05:01