猿问

在矩阵的第 N 列中查找最大/最小元素索引的快速方法

我正在尝试获取矩阵中特定列的最大和最小元素的索引。现在我使用 ES6 和 Spread 语法执行以下操作:


a = [

  [22,23],

  [74,1],

  [21,33],

  [32,84],

  [11,31],

  [1,49],

  [7,8],

  [11,11],

  [99,68],

  [52,20]

];


const minValue = (arr, n) => Math.min(...arr.map(x => x[n])); //n - column index

const maxValue = (arr, n) => Math.max(...arr.map(x => x[n]));


const minValueIndex = (arr, n) => arr.map(x => x[n]).indexOf(minValue(arr, n));

const maxValueIndex = (arr, n) => arr.map(x => x[n]).indexOf(maxValue(arr, n));


console.log(minValue(a, 0));

console.log(maxValue(a, 0));


console.log(minValueIndex(a, 0));

console.log(maxValueIndex(a, 0));


杨魅力
浏览 191回答 3
3回答

慕仙森

一个简单的解决方案是遍历数组并将最小值/最大值存储在临时变量中。function minMax (arr, n) {&nbsp; &nbsp; let min=Infinity, max=0;&nbsp; &nbsp; for (const _arr of arr) {&nbsp; &nbsp; &nbsp; &nbsp; const x = _arr[n];&nbsp; &nbsp; &nbsp; &nbsp; if (x < min) min = x;&nbsp; &nbsp; &nbsp; &nbsp; if (x > max) max = x;&nbsp; &nbsp; }&nbsp; &nbsp; return [min, max];}function minMaxIndex (arr, n) {&nbsp; &nbsp; let min=Infinity, max=0, minIndex, maxIndex;&nbsp; &nbsp; for (let i=0; i < arr.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; const x = arr[i][n];&nbsp; &nbsp; &nbsp; &nbsp; if (x < min) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; min = x;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; minIndex = i;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (x > max) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max = x;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxIndex = i;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return [minIndex, maxIndex];}console.log (minMax(a, 0))console.log (minMaxIndex(a, 0))<script>a = [&nbsp; [22,23],&nbsp; [74,1],&nbsp; [21,33],&nbsp; [32,84],&nbsp; [11,31],&nbsp; [1,49],&nbsp; [7,8],&nbsp; [11,11],&nbsp; [99,68],&nbsp; [52,20]];</script>

动漫人物

你快到了,你只关心性能,对吧?因此,为了提高程序的性能,您可以使用一种名为Memoization记忆是一种优化技术,主要用于通过存储昂贵的函数调用的结果并在再次出现相同的输入时返回缓存的结果来加速计算机程序const arr = [[22,23], [74,1], [21,33], [32,84], [11,31], [1,49], [7,8], [11,11], [99,68], [52,20]];/*** Here I create the momoized function which cache the* column and if we want to get the same column then it* simply return the previously cached column array* otherwise, it get the column and cache it for future* and return it.*/const memoized = () => {&nbsp; &nbsp; const cache = {};&nbsp; &nbsp; return (arr, index) => {&nbsp; &nbsp; &nbsp; &nbsp; if (index in cache) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return cache[index];&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; const col = arr.map(item => (item[index]));&nbsp; &nbsp; &nbsp; &nbsp; cache[index] = col;&nbsp; &nbsp; &nbsp; &nbsp; return col;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}/*** As memoized is a higher order function so it returns* another function which will be executed by calling* this getColumn function reference.*/const getColumn = memoized();const getMinValue = (arr, col) => Math.min(...getColumn(arr, col));const getMaxValue = (arr, col) => Math.max(...getColumn(arr, col));const minValueIndex = (arr, col) => getColumn(arr, col).indexOf(getMinValue(arr, col));const maxValueIndex = (arr, col) => getColumn(arr, col).indexOf(getMaxValue(arr, col));console.log('minValue: ', getMinValue(arr, 0)); // Calculatedconsole.log('maxValue: ',getMaxValue(arr, 0)); // Cachedconsole.log('minValueIndex: ', minValueIndex(arr, 0)); // Cachedconsole.log('maxValueIndex: ', maxValueIndex(arr, 0)); // Cached.as-console-wrapper {min-height: 100% !important; top: 0;}

慕田峪7331174

这会有帮助吗?let a = [&nbsp; &nbsp; [22, 23],&nbsp; &nbsp; [74, 1],&nbsp; &nbsp; [21, 33],&nbsp; &nbsp; [32, 84],&nbsp; &nbsp; [11, 31],&nbsp; &nbsp; [1, 49],&nbsp; &nbsp; [7, 8],&nbsp; &nbsp; [11, 11],&nbsp; &nbsp; [99, 68],&nbsp; &nbsp; [52, 20]];let max = 0,&nbsp; &nbsp; min = 0,&nbsp; &nbsp; minIndex = 0,&nbsp; &nbsp; maxIndex = 0;const findValue = (array, col) => {&nbsp; &nbsp; array.map((matrix) => {&nbsp; &nbsp; &nbsp; &nbsp; (matrix[col] > max) ? max = matrix[col]: null;&nbsp; &nbsp; &nbsp; &nbsp; (min == 0) ? min = max: null;&nbsp; &nbsp; &nbsp; &nbsp; (matrix[col] < min) ? min = matrix[col]: null;&nbsp; &nbsp; })}const findIndex = (array, col, min, max) => {&nbsp; &nbsp; minIndex = array.map(data => data[col]).indexOf(min);&nbsp; &nbsp; maxIndex = array.map(data => data[col]).indexOf(max);}findValue(a, 0)findIndex(a, 0, min, max);console.log(min, max, minIndex, maxIndex);
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答