在无序整数数组中查找最大对和的函数

我想写一个函数,给定一个无序数字序列,找到最大的对和。


largestPairSum([10, 14, 2, 23, 19]) --> 42 (i.e. sum of 23 and 19)

largestPairSum([99, 2, 2, 23, 19])  --> 122 (i.e. sum of 99 and 23)

largestPairSum([-10,-20,-30,-40])   --> -30 (i.e sum of -10 and -20)

我的尝试


function largestPairSum(numbers)

{

   let counter =0;

   let numbersord = numbers.sort();


   if (numbersord[0] === -numbersord[0]) {

       numbersord.reverse()

       counter=numbersord[0]+numbersord[1]

       }


   else {

       counter=numbersord[-1]+numbersord[-2]

    }

  return counter

}

当调用时,该函数说“NaN”,但是当我



console.log(typeof(numbersord[0]))


它说数字。不知道我哪里做错了,感谢您的阅读!


慕神8447489
浏览 120回答 4
4回答

萧十郎

你的方法不起作用,因为按字符串升序排序(标准无回调)使用负指数。你可以按降序排序,并取前两个埃勒姆特。function largestPairSum(numbers) {&nbsp; &nbsp; numbers.sort((a, b) => b - a);&nbsp; &nbsp; return numbers[0] + numbers[1];}console.log(largestPairSum([10, 14, 2, 23, 19])); // 42 (23 + 19)console.log(largestPairSum([99, 2, 2, 23, 19]));&nbsp; // 122 (99 + 23)console.log(largestPairSum([-10, -20, -30, -40])) // -30 (-10 + -20)无需排序的解决方案。function largestPairSum(numbers) {&nbsp; &nbsp; let largest = numbers.slice(0, 2),&nbsp; &nbsp; &nbsp; &nbsp; smallest = largest[0] < largest[1] ? 0 : 1;&nbsp; &nbsp; for (let i = 2; i < numbers.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (largest[smallest] > numbers[i]) continue;&nbsp; &nbsp; &nbsp; &nbsp; largest[smallest] = numbers[i];&nbsp; &nbsp; &nbsp; &nbsp; smallest = largest[0] < largest[1] ? 0 : 1;&nbsp; &nbsp; }&nbsp; &nbsp; return largest[0] + largest[1];}console.log(largestPairSum([10, 14, 2, 23, 19])); // 42 (23 + 19)console.log(largestPairSum([99, 2, 2, 23, 19]));&nbsp; // 122 (99 + 23)console.log(largestPairSum([-10, -20, -30, -40])) // -30 (-10 + -20)

沧海一幻觉

在 中,如@VLAZ所建议:O(n)const largestPairSum = (arr) => {&nbsp; let a = -Infinity,&nbsp; &nbsp; &nbsp; b = -Infinity;&nbsp;&nbsp;&nbsp; for (let item of arr)&nbsp; &nbsp; if (item > a && b > -Infinity)&nbsp; &nbsp; &nbsp; a = item;&nbsp; &nbsp; else if (item > b)&nbsp; &nbsp; &nbsp; b = item;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; return a+b;}let tests = [&nbsp; largestPairSum([10, 14, 2, 23, 19]),&nbsp; largestPairSum([99, 2, 2, 23, 19]),&nbsp; largestPairSum([-10,-20,-30,-40]),];console.log(tests);console.log("Nina's test:", largestPairSum([20, 50, 10, 1, 2]));

蝴蝶不菲

代码中存在三个问题。首先,是不正确的。尝试从数组中获取负索引,只是返回 ,因为那里什么都没有。它不是从最后翻转和获取,要做到这一点,你需要明确地传递从最后获取内容:counter=numbersord[-1]+numbersord[-2]undefinedarrayLength - negativeIndexconst arr = ["a", "b", "c", "d", "e"];console.log(arr[1]);console.log(arr[2]);console.log(arr[-1]);console.log(arr[-2]);console.log(arr[arr.length - 1]);console.log(arr[arr.length - 2]);第二,是不正确的。它使用词典顺序进行排序,而不是数字,其中还有 。您需要正确对数字进行排序(请查看链接以获取更多信息):numbers.sort()1 < 210 < 2const arr = [1, 2, 10, 20]arr.sort();console.log("default (lexicographical) sort", arr);arr.sort((a, b) => a - b);console.log("ascending sort", arr);arr.sort((a, b) => b - a);console.log("descending sort", arr);最后,这种情况是无用的。唯一等于自身负数的数字是零:if(numbersord[0] === -numbersord[0])console.log(0 === -0);console.log(1 === -1);console.log(42 === -42);console.log(Infinity === -Infinity);console.log(NaN === -NaN);但是,检查这没有用。那里的逻辑(基本上)检查数组是否以零开头,如果是,它将反转它并获取前两个结果。如果它不以零开头,它将尝试获取数组的最后两个元素。但是,如果您只是按降序排序,则可以免费获得所有这些内容,并且每次都可以获取前两个项目。因此,通过这些更改,您的代码如下所示function largestPairSum(numbers){&nbsp; &nbsp;let counter =0;&nbsp; &nbsp;//perform a descending sort&nbsp; &nbsp;let numbersord = numbers.sort((a, b) => b - a);&nbsp; &nbsp;&nbsp; &nbsp;//always take the first two items&nbsp; &nbsp;counter=numbersord[0]+numbersord[1]&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;return counter}console.log(largestPairSum([10, 14, 2, 23, 19]))// --> 42 (i.e. sum of 23 and 19)console.log(largestPairSum([99, 2, 2, 23, 19])) // --> 122 (i.e. sum of 99 and 23)console.log(largestPairSum([-10,-20,-30,-40]))&nbsp; // --> -30 (i.e sum of -10 and -20)

守着星空守着你

NaN类型是一个数字,它是正确的。您之所以得到这个消息,是因为任何数组中都没有索引为“-1”或“-2”的元素。如果你想从最后得到一个元素,你必须使用类似的东西arr[arr.length - 1] // returns last element然后let numbersord = numbers.sort(); // sort() method mutates the original array, be carefulnumbersord[0] === -numbersord[0] // - returns true only when number is 0// to check if it is negative use Math.abs()问题是 - 数组可以同时包含负元素和正元素吗?在这种情况下,您不能只是反转数组 [-10, -5, -2, 10] - 最大和是 8,而不是 -15。我会使用这样的简化方法:function largestPairSum(arr) {&nbsp; const initialAcc = [-Infinity, -Infinity]&nbsp; const highestNumbers = arr.reduce((acc, rec)=> {&nbsp; &nbsp; if (rec <= acc[0]) return acc&nbsp; &nbsp; if (rec >= acc[1]) return [acc[1], rec]&nbsp; &nbsp; return [rec, acc[1]]&nbsp; },initialAcc)&nbsp; return highestNumbers[0] + highestNumbers[1]}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript