Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 选中列表中的连续元素_Java - Fatal编程技术网

Java 选中列表中的连续元素

Java 选中列表中的连续元素,java,Java,我对检查列表中连续元素的JAVA算法有问题 例如: 列表:[1,2,3,3,4,5,3,3]=>我想要:数字3连续两次的次数是多少?结果:2 这是我的代码,但我没有一个好的结果 公共类检查连续元素{ /** * Returns the number of groups containing "numOfOccs" occurrences of element "elem" passed as argument. * * @param numOfOccs the exact num

我对检查列表中连续元素的JAVA算法有问题

例如: 列表:[1,2,3,3,4,5,3,3]=>我想要:数字3连续两次的次数是多少?结果:2

这是我的代码,但我没有一个好的结果

公共类检查连续元素{

/**
 * Returns the number of groups containing "numOfOccs" occurrences of element "elem" passed as argument.
 *
 * @param  numOfOccs     the exact number of consecutive occurrences
 * @param  elem            the element to search occurrences for
 * @param  whereToFind   the array to search for repetitions
 * @return number of groups with "numOfOccs" consecutive repetitions of element "elem"
 */

public int nGroup(int numOfOccs,int elem, int[] whereToFind) {
    int contador=0;
    if (numOfOccs==0 || whereToFind==null) //case null
        return contador;
    for (int i=0; i<whereToFind.length; i++){
        if(whereToFind[i]!=elem)  //if "elem" doesn't appear on the array
            return contador;
        for (int j=1; j<whereToFind.length; j++){
            if(whereToFind[i]==elem && (whereToFind[i]==whereToFind[j]))
                contador++;
        }

    }

    return contador;
}
示例:

nGroup0,2,{1,2,2,3,2}==>0

n组1,2,{1,2,2,3,2}=>4

n组2,2,{1,2,2,3,2}==>2

n组3,2,{1,2,2,3,2}==>1

n组4,2,{1,2,2,3,2}=>0


你能帮我吗?谢谢…

这里有一段代码可以满足你的要求:

private static int nGroup(final int numOfOccs, final int elem, final int[] whereToFind) {
    if(numOfOccs == 0 || whereToFind == null) {
        return 0;
    }

    int res = 0;
    int streakLength = 0;
    for (final int i : whereToFind) {
        if (elem != i) {
            streakLength = 0;
        } else if (++streakLength >= numOfOccs) {
            res++;
        }
    }

    return res;
}
我会给出一些伪代码和一些解释,但我相信算法足够简单,任何人都能理解。如果没有,请告诉我,我会相应地更新我的答案

针对您的测试用例运行:

System.out.println(nGroup(0, 2, new int[] { 1, 2, 2, 2, 3, 2 }));
System.out.println(nGroup(1, 2, new int[] { 1, 2, 2, 2, 3, 2 }));
System.out.println(nGroup(2, 2, new int[] { 1, 2, 2, 2, 3, 2 }));
System.out.println(nGroup(3, 2, new int[] { 1, 2, 2, 2, 3, 2 }));
System.out.println(nGroup(4, 2, new int[] { 1, 2, 2, 2, 3, 2 }));
…将产生以下输出:

0
4
2
1
0

以下是我所做的,以及一些测试:

 public static void main( String[] args ) {
        System.out.println(nGroup(0,2,new int[]{1,2,2,2,3,2}));   //0
        System.out.println(nGroup(1,2,new int[]{1,2,2,2,3,2}));   //4
        System.out.println(nGroup(2,2,new int[]{1,2,2,2,3,2}));   //2
        System.out.println(nGroup(3,2,new int[]{1,2,2,2,3,2}));   //1
        System.out.println(nGroup(4,2,new int[]{1,2,2,2,3,2}));   //0
        System.out.println(nGroup(2,2,new int[]{1,2,2,2,2,3,2})); //3
    }

    public static int nGroup(int nOccs, int val, int[] arr) {
        int count = 0;
        if (nOccs == 0 || arr == null)
            return count;

        for (int i=0; i<arr.length; i++) {
            int occs;
            for (occs = 0 ; i<arr.length && arr[i] == val ; i++, occs++);
            if (occs >= nOccs) count += occs - nOccs + 1;
        }
        return count;
    }

nGroup2,2,{1,2,2,2,2,3,2}的预期输出是什么?简言之,如何处理重复m>numofocs次的值?该值的预期输出是:3,因为您可以采用2个数字2的3组。要更好地查看NgroupNumberOfContinueReplications,NumberToSearch,[Array]我认为这有点过于复杂了:你可以简单地用一个for循环,每次遇到一个等于elem的元素时,递增计数为1,我们所处的条纹至少是numofocs元素。如果你想看看的话,我的答案是一个IMO更简单的方法;@ccjmne:这不是那个compl这是一种不同的方式,它有着相同的时间复杂性。谢谢,我的失败是我没有考虑任何NoNoFoCS,正如我在你的代码中看到的。没问题!很高兴帮助你。
 public static void main( String[] args ) {
        System.out.println(nGroup(0,2,new int[]{1,2,2,2,3,2}));   //0
        System.out.println(nGroup(1,2,new int[]{1,2,2,2,3,2}));   //4
        System.out.println(nGroup(2,2,new int[]{1,2,2,2,3,2}));   //2
        System.out.println(nGroup(3,2,new int[]{1,2,2,2,3,2}));   //1
        System.out.println(nGroup(4,2,new int[]{1,2,2,2,3,2}));   //0
        System.out.println(nGroup(2,2,new int[]{1,2,2,2,2,3,2})); //3
    }

    public static int nGroup(int nOccs, int val, int[] arr) {
        int count = 0;
        if (nOccs == 0 || arr == null)
            return count;

        for (int i=0; i<arr.length; i++) {
            int occs;
            for (occs = 0 ; i<arr.length && arr[i] == val ; i++, occs++);
            if (occs >= nOccs) count += occs - nOccs + 1;
        }
        return count;
    }