简体   繁体   English

Java - 帮助理解编码代码

[英]Java - Assistance with understanding codility code

For the PermCheck codility test, I coded one solution (please see below) but it only really solved the example given in the codility test because there are only a few values in the array and small values.对于 PermCheck codility 测试,我编写了一个解决方案(请参见下文),但它仅真正解决了 codility 测试中给出的示例,因为数组中只有几个值且值很小。 I also added code below which scored 100%, which is code I found on the internet.我还在下面添加了得分为 100% 的代码,这是我在互联网上找到的代码。 That code looks very different from mine and I couldn't work out how he/she was able to get the answer.该代码看起来与我的非常不同,我无法弄清楚他/她是如何得到答案的。 Could someone please explain the code step by step and how it results in the answer please.有人可以请逐步解释代码以及它是如何产生答案的。

Codility Test: Codility 测试:

PermCheck永久检查

Check whether array A is a permutation.检查数组 A 是否为排列。

A non-empty zero-indexed array A consisting of N integers is given.给出了一个由 N 个整数组成的非空零索引数组 A。

A permutation is a sequence containing each element from 1 to N once, and only once.置换是包含从 1 到 N 的每个元素一次且仅一次的序列。

For example, array A such that:例如,数组A使得:

A[0] = 4
A[1] = 1
A[2] = 3
A[3] = 2

is a permutation, but array A such that:是一个排列,但数组A使得:

A[0] = 4
A[1] = 1
A[2] = 3

is not a permutation, because value 2 is missing.不是排列,因为缺少值 2。

The goal is to check whether array A is a permutation.目标是检查数组A是否是排列。

Write a function:写一个函数:

class Solution {
    public int solution(int[] A);
}

that, given a zero-indexed array A , returns 1 if array A is a permutation and 0 if it is not.给定一个零索引数组A ,如果数组A是排列,则returns 1否则returns 1 0

For example, given array A such that:例如,给定数组 A 使得:

A[0] = 4
A[1] = 1
A[2] = 3
A[3] = 2

the function should return 1.该函数应返回 1。

Given array A such that:给定数组 A 使得:

A[0] = 4
A[1] = 1
A[2] = 3

the function should return 0.该函数应返回 0。

Assume that:假使,假设:

  • N is an integer within the range [1..100'000]; N 是 [1..100'000] 范围内的整数;
  • Each element of array A is an integer within the range [1..1'000'000'000].数组 A 的每个元素都是 [1..1'000'000'000] 范围内的整数。

Complexity:复杂:

  • Expected worst-case time complexity is O(N)预期的最坏情况时间复杂度为 O(N)
  • Expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).预期的最坏情况空间复杂度为 O(N),超出输入存储(不包括输入参数所需的存储)。
  • Elements of input arrays can be modified.可以修改输入数组的元素。

100% Score Solution (found from internet): 100% 分数解决方案(来自互联网):

public static final int NOT_PERMUTATION = 0;
public static final int PERMUTATION = 1;
// (4,1,3,2) = 1
// (4,1,3) = 0
// (1) = 1
// () = 1
// (2) = 0
public int PermSolution(int[] A) {
    // write your code in Java SE 8
    int[] mark = new int[A.length + 1];
    int counter = 0;

    for (int i = 0; i < A.length; ++i) {
        int value = A[i];
        if(value >= mark.length) {
            return NOT_PERMUTATION;
        }
        if(mark[value] == 0) {
            mark[value]=1;
            ++counter;
        } else {
            return NOT_PERMUTATION;
        }
    }

    return counter == A.length ? PERMUTATION : NOT_PERMUTATION;
}

My Solution:我的解决方案:

public int PermSolution(int[] A)
{
    int perm = 1;

    Arrays.sort(A);

    if (A[0] != 1) return 0;

    for (int i = 0; i < A.length; i++)
    {
        if (A[i] + 1 == A[i + 1])
        {
            return perm;
        }

        if (A[i] + 1 != A[i + 1])
        {
            return 0;
        }
    }

    return perm;

}

Using Arrays.sort() is kind of original, that's not how I would have done it though.使用Arrays.sort()是一种原创,但我不会这样做。

To comment your code, it's probably isn't working because of this : return perm;要评论您的代码,它可能无法正常工作,因为: return perm;

Let's say you have this Array which is not a permutation:假设您有这个不是排列的数组:

A[0] = 4
A[1] = 1
A[2] = 2

One you execute Arrays.sort(A) , you'll have this :你执行Arrays.sort(A) ,你会得到这个:

A[0] = 1
A[1] = 2
A[2] = 4

Now let's execute your code :现在让我们执行你的代码:

if (A[0] != 1) return 0;

A[0] is indeed equal to 1 Next, for i==0 we have : A[0]确实等于1接下来,对于i==0我们有:

  if (A[i] + 1 == A[i + 1])
    {
        return perm;
    }

A[i] + 1 is equal 2 and A[i+1] is also equal to 2 the condition being true , you execute a return perm; A[i] + 1等于2并且A[i+1]也等于2如果条件为true ,则执行return perm; and thus, you end your execution with a return 1 .因此,您以return 1结束执行。

Actually, as long as your array contains 1 and 2 , this function will always return 1实际上,只要您的数组包含12 ,此函数将始终return 1

For it to work, you'll have to check all of the array before actually returning a value.要使其正常工作,您必须在实际返回值之前检查所有数组。

This should work :这应该工作:

public int PermSolution(int[] A)
{

int perm = 1;
Arrays.sort(A);

if (A[0] != 1) return 0;

for (int i = 0; i < A.length; i++)
{
       if (A[i] + 1 != A[i + 1])
    {
        return 0;
    }
}

return perm;

}

To optimize it even further, this should work as well :为了进一步优化它,这也应该有效:

public int PermSolution(int[] A)
    {

    Arrays.sort(A);

    for (int i = 0; i < A.length; i++)
    {
           if (A[i] != i+1)
        {
            return 0;
        }
    }

    return 1;

    }

Why don't we avoid Arrays.sort(A) in order to gain the computation efficiency by below:我们为什么不避免 Arrays.sort(A) 以通过以下方式获得计算效率:

public static int PermSolution(int[] A)
{
    int len=A.length;
    if(len==1)
        return A[0]==1 ? 1 : 0;

    BitSet set=new BitSet(len+2);

    for (int i = 0; i < len; i++)
    {
        if(A[i]>len || set.get(A[i]))
            return 0;

        set.set(A[i]);
    }
    return set.nextClearBit(1)==(len+1) ? 1 : 0;
}

Here is some solution that I have developed.这是我开发的一些解决方案。 Not sure about constraints, if someone can help to test it against them.不确定约束,如果有人可以帮助对它们进行测试。 Thanks people!谢谢人们!

private static int solution(int[] arr) {

        int perm=1;
        boolean b=false;
        Arrays.sort(arr);
        int i=0;
        while (i<=arr.length) {
            if(i < arr.length-2)
                b = arr[i+1]-1 == (arr[i]);
            if(b) {
                System.out.println("if " + i);
                i++;
                perm=1;
            }else {
                System.out.println("else " + i);
                perm = 0;
                break;
            }
        }   
        return perm;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM