作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在努力应对来自 Codility 的挑战,我不明白为什么 MinAbsSumOfTwo
挑战在 C# 中使用相同的算法,但在 Javascript 中不起作用。
这是 link到 JS 实现的结果页面 (54/100)。
这里是 link到 C# (100/100) 实现的结果页面。
我试图在我的机器上模拟 extreme_large
和 arithmetic_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/
我一直在努力应对来自 Codility 的挑战,我不明白为什么 MinAbsSumOfTwo 挑战在 C# 中使用相同的算法,但在 Javascript 中不起作用。 这是 link到 JS 实现的结
我是一名优秀的程序员,十分优秀!