Codility lesson 1 TapeEquilibrium for java显示33%

时间:2015-06-12 06:50:23

标签: java

public int solution(int[] A) {
    int lengthOfArray = A.length;
    int tempArray[] = new int[lengthOfArray];
    System.arraycopy(A, 1, tempArray, 0,lengthOfArray-1);
    int head = A[0];
    int tail = 0;
    for(int arrElm : tempArray){
        tail += Math.abs(arrElm);
    }
    double minDifference = Math.abs(tail-head);
    for(int i = 1; i<lengthOfArray; i++){
        head += A[i];
        tail -= A[i];
        double tempDifference = Math.abs(tail - head);
        if(tempDifference < minDifference){
            minDifference = tempDifference;
        }
    }
    return (int) minDifference;
}

注意:上述解决方案仅显示33%,对于负数据,它的正确性失败。

我不确定我在做错的地方。

3 个答案:

答案 0 :(得分:1)

我尝试了你的代码。我认为你的问题在于计算时间:

for(int arrElm : tempArray){
    tail += Math.abs(arrElm);
}

你不应该使用绝对值。所以它应该是:

tail +=arrElm

除此之外,我看到的另一个问题是for循环:

for(int i = 1; i<lengthOfArray; i++)

应该是:

for(int i = 1; i<lengthOfArray-1; i++)

否则输入[-1,1]将返回0而不是2。

答案 1 :(得分:0)

使用 Math.abs 会给您不正确的结果;输入值应按原样使用。

Math.abs - 返回int值的绝对值。如果参数不是负数,则返回参数。如果参数为负数,则返回参数的否定。

同样,不需要使用 System.arraycopy 。 PFB更正的代码:

public int solution(int[] A) {
    int lengthOfArray = A.length;
    int head = A[0], tail = 0;

    for (int j = 1; j < lengthOfArray; j++)
        tail += A[j];

    double minDifference = Math.abs(tail - head);

    for (int i = 1; i < lengthOfArray; i++) {
        head += A[i];
        tail -= A[i];
        double tempDifference = Math.abs(tail - head);

        if (tempDifference < minDifference)
            minDifference = tempDifference;
    }
    return (int) minDifference;
}

答案 2 :(得分:0)

如果你仔细查看任务描述,你会发现A数组中的元素在[-1000,1000]范围内,所以你可以使用 short 而不是 int ,以节省空间和处理能力。

可以将值保持在[-32768,32767]内,而 int 在[-2147483648,2147483647]内]。对于数组A中的值,两者都太大了,为什么要使用不必要的更大类型?保持较短的数字不仅占用了一半的存储空间(16位而不是32位),而且还需要较少的处理能力来操作。

由于你必须最初遍历整个A数组以获得其元素的总和,你可以将它映射到同一循环中的short数组。

并且不需要为tempDifference使用double - A数组的所有元素都是整数。不可能从减法或添加两个整数中获得浮点数。

这得到100%/ 100%的结果:

Msg 156, Level 15, State 1, Line 9
Incorrect syntax near the keyword 'PROCEDURE'.
Msg 137, Level 15, State 2, Line 31
Must declare the scalar variable "@date".

另外,如上所述:

public int solution(int[] A) {
    short[] shortA=new short[A.length]; //this will hold a copy of int[] A, but instead of integer it uses short
    int sum1stSlice=A[0],               //sum of first slice of the A array, initially it contains just the first element
        sum2ndSlice=0,                  //we'll get a sum of the remaining part of the A array later
        minimalDifference,
        difference;
    for(int i=0;i<A.length;i++){        //iterating throug the whole A array for the first time
        shortA[i]=(short)A[i];          //mapping values in A kept as int to values in our own array, keeping them as short
        sum2ndSlice+=shortA[i];         //two birds with one stone - we use the same iteration for getting the sum of all elements
    }
    sum2ndSlice-=sum1stSlice;           //sum of all elements but the first one - now it's truly sum2ndSlice
    minimalDifference=Math.abs(sum1stSlice-sum2ndSlice);    //initially the minimal difference will be the current difference
    for(int i=1;i<shortA.length-1;i++){         //iterating through all elements of our newly created, more compact version of array A: shortA
        sum1stSlice+=shortA[i];                 //moving one element to the sum of first slice
        sum2ndSlice-=shortA[i];                 //removing that element from the sum of second slice
        difference=Math.abs(sum1stSlice-sum2ndSlice);       //getting the difference between the sums of these slices
        if(difference<minimalDifference)minimalDifference=difference;   //checking for the new minimum
    }
    return minimalDifference;
}

不正确,因为任务描述清楚地表明元素可以是负的,在[-1000,1000]内,并且使用Math.abs将获得[-1000,0]内所有内容的加法逆。