java这个解决方案有什么问题?
一些在线测试的问题:
无法复制该问题,因为它受版权保护。问题可在此找到: http://codility.com/c/run/demo534WQK-UBH
我们可以从示例中看到:
P = 1, difference = |3 − 10| = 7
P = 2, difference = |4 − 9| = 5
P = 3, difference = |6 − 7| = 1
P = 4, difference = |10 − 3| = 7
元素之和总是常数(3+10)=(4+9)=(6+7)等等。我们还注意到左侧的数字增加,而右侧的数字减少
我的解决方案:
class Solution
{
public int solution(int A[])
{
long sum = 0;
for (int i = 0; i < A.length; i++)
{
sum += A[i];
}
long currSum = 0;
long ans = Long.MAX_VALUE;
for (int i = 0; i < A.length; i++)
{
currSum += A[i];
long diff = Math.abs((sum - currSum) - currSum);
ans = Math.min(ans, diff);
}
return (int) ans;
}
}
http://codility.com/demo/results/demo8MC2GU-7BK/
现在有没有办法,我可以找出为什么我的解决方案在两个测试用例中失败?我知道可能无法看到输入的内容,所以只能依靠代码检查
# 1 楼答案
问题是你答案中的“p”变量是0<;P<;=N
但要求是0<;P<;N
因此,您需要将秒循环从
到