gpt4 book ai didi

javascript - Javascript 中的 Codility MinAbsSumOfTwo

转载 作者:行者123 更新时间:2023-11-30 21:13:09 24 4
gpt4 key购买 nike

我一直在努力应对来自 Codility 的挑战,我不明白为什么 MinAbsSumOfTwo 挑战在 C# 中使用相同的算法,但在 Javascript 中不起作用。

这是 link到 JS 实现的结果页面 (54/100)。

这里是 link到 C# (100/100) 实现的结果页面。

我试图在我的机器上模拟 extreme_largearithmetic_medium 测试,但两者都给了我预期的结果:

  it('should return 1002 for arithmetic medium', () =>
{
const arithmetic = []
let value = -5010000

for(let i = 0; i < 10000; i++)
arithmetic.push(value += 1002)

assert.equal(solution(arithmetic), 1002)
})

it('should return 1999999994 for sequence of MAX_INT', () =>
{
const maxIntSequence = []

for(let i = 0; i < 100000; i++)
maxIntSequence.push(999999997)

assert.equal(solution(maxIntSequence), 1999999994)
})

如果有人能提供帮助,我将不胜感激!

最佳答案

您的absOrder 方法无效; Array.sort期望比较器函数返回一个数字,而不是 bool 值:

A.sort((a,b) => Math.abs(a) - Math.abs(b))

关于javascript - Javascript 中的 Codility MinAbsSumOfTwo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45928372/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com