找出用于在数组中找到独特出现的算法的情况
我必须进行编码算法测试,并选择了一个问题以训练,但是,我无法弄清楚为什么我的算法失败。
请注意,失败后,我进行了一些研究,显然仅置换了所有值是一个更好的解决方案,但是它仅适用于可操作的元素(具有匹配的行为),因此我有兴趣改用解决方案。
这是失败测试的链接: https:// https:// https://app.codility.com/demo /resuction/triendingd7wdbd-jgp/
基本上,您必须找到一个在所有其他数字发生不止一次发生的数组中仅发生一次的数字。这是我评论的解决方案: (C#,但应该没关系)
static public int solution(int[] A)
{
// the test guarantees non-empty input and odd length
if (A.Length <= 2) // ofc we could check for == 1 and throw on == 2
return A[0];
Array.Sort(A);
if (A[0] != A[1]) // if 1st element is different than 2nd it's unique
return A[0];
for (int i = 2; i < A.Length; i++)
{
var previous = A[i-2];
var current = A[i-1];
var next = A[i];
// we process 3 elements at a time, if they are all different
// the middle one is the unique... we don't test the last
if (previous != current && current != next)
return current;
}
// we don't test the last element, so if we drop out of the loop
// it must be the unique element... we could check that and throw
// if it's not unique but the test description says that won't happen
return A[A.Length-1];
}
但是,代码未能通过某些测试,但我不知道这些失败情况是什么,这是我本地运行的SSCCE,而且那里的所有测试显然都很好: htttps://
,可以你们帮助我确定未正确处理哪些输入?编码结果没有这些信息,只是一些输入失败(Small1,Medion2,Big1)。谢谢你!
I have to do a Codility algorithm test and I picked a question in order to train, however, I can't figure why my algorithm is failing.
Please note that after the fail I did some research and obviously just XORing all values is a better solution, but it only applies to elements that are bitwise operable (with matching behavior), so I'm interested in fixing my solution instead.
Here's the link to the failed test: https://app.codility.com/demo/results/trainingD7WDBD-JGP/
Basically, you have to find a number that occurs only once in an array where all other numbers occur more than once. Here's my commented solution:
(C# but shouldn't matter much)
static public int solution(int[] A)
{
// the test guarantees non-empty input and odd length
if (A.Length <= 2) // ofc we could check for == 1 and throw on == 2
return A[0];
Array.Sort(A);
if (A[0] != A[1]) // if 1st element is different than 2nd it's unique
return A[0];
for (int i = 2; i < A.Length; i++)
{
var previous = A[i-2];
var current = A[i-1];
var next = A[i];
// we process 3 elements at a time, if they are all different
// the middle one is the unique... we don't test the last
if (previous != current && current != next)
return current;
}
// we don't test the last element, so if we drop out of the loop
// it must be the unique element... we could check that and throw
// if it's not unique but the test description says that won't happen
return A[A.Length-1];
}
That code however is failing some tests, but I can't figure what are these fail cases, here's a SSCCE I'm running locally and all the tests there are apparently fine:
https://gist.github.com/Alan-FGR/d65e55c9aebca4c48c5d69615d105cdb
So, can you guys help me figure what inputs are not being handled correctly? The Codility results don't have that information, just that a few inputs failed (small1, medium2, big1). Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您忘记了奇怪的元素可能不止一次发生,只需要奇怪的次数才能使您的测试失败。正如您在我的示例中看到的那样:
可能您没有考虑到
I think you forgot that the odd element CAN occur more than once, it just have to be odd number of times for your test to fail. As you can see in my example here:
Probably you didn't take that in account