-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
第 161 题:用最精炼的代码实现数组非零非负最小值index #421
Comments
/**
* @description 用最精炼的代码实现数组非零最小值 index
* @param {array} arr 数组
* @returns {number} index 索引
*/
function getIndex(arr) {
let index = -1;
const minVal = arr.reduce((cur, pre) => {
return (cur <= 0 || pre <= 0) ? Math.max(cur, pre) : cur > pre ? pre : cur;
}, -1);
index = arr.findIndex(item => item == minVal && minVal > 0)
return index;
} |
@slogeor reduce和reduce一起复杂度是多少? 没有体现最精炼 |
@libin1991 上面的时间复杂度是 O(N^2), function getIndex(arr) {
let index = -1;
arr.reduce((pre, cur, k) => {
if (cur <= 0 || pre <= 0) {
index = cur <= 0 && pre <= 0 ? -1 : cur > pre ? k : index;
} else {
index = cur > pre ? index : k;
}
return cur > pre ? pre : cur;
}, -1);
return index;
} 时间复杂度是 O(N) |
import postwroker from 'worker-loader!./postworker' |
@tanxin520 上下文方便说一下嘛 |
https://www.webpackjs.com/loaders/worker-loader/ |
这个时间复杂度也不低 |
@libin1991 楼主,我是否可以贡献一题,实现 JSON.stringify 方法 |
输出答案都是错的!
|
调整了一下 function getIndex(arr) {
if (!arr.length) return -1;
let index = -1;
arr.reduce((pre, next, k) => {
const min = Math.min(pre, next);
const max = Math.max(pre, next, 0);
// 都是正数
if (pre > 0 && next > 0) {
index = min === pre ? index : k
return min;
}
index = (max === 0 || max === pre) ? index : k;
return max;
}, 0)
return index;
} |
rankVoteMaxIndex([-1] // 0 |
|
会不会B格不够高 const arr = [-10, 0, 10, 21, 0, -7, 35, 7, 9, 23, 18]
let minNum
let minIndex = -1
for (let i = 0, len = arr.length; i < len; i++) {
if (minNum) {
if (arr[i] < minNum && arr[i] > 0) {
minNum = arr[i]
minIndex = i
}
} else {
if (arr[i] > 0) {
minNum = arr[i]
minIndex = i
}
}
}
console.log(minIndex, minNum) |
const minIndex = (arr) => arr.reduce((num, v, i) => v > 0 && v < arr[num] ? i : num, 0) |
先找出大于0的数,然后排序,最小值就是非负非零的最小值,楼上的这个也算一个方法 |
初始值为-1的话是没法找到最小值的,经测试这条式子有点问题,做了一点调整,代码如下 // 先找到第一个非负非零的值的下标
function findInitialValue(arr) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] > 0) return i;
}
return -1;
}
const arr = [10, 21, 0, -7, 35, 7, 9, 23, 18];
function minIndex(arr) {
let first = findInitialValue(arr);
// 无非负非零的数,直接返回-1
if (first === -1) return -1;
return arr.reduce(
(num, cur, curIndex) => (cur > 0 && cur < arr[num] ? curIndex : num),
// 关键是reduce的初始值
first
);
}
console.log(minIndex(arr)); // 5 reduce用法相关参考链接: |
function getIndex(arr) {
return arr.reduce((idx, cur, k) => idx = cur <= idx[1] && cur > 0 ? [k, cur] : idx, [-1, Number.MAX_SAFE_INTEGER])[0]
} |
code: arr.reduce((res, value, index) => value > 0 && (res === -1 || arr[index] < arr[res]) ? index : res, -1) |
实现的比较low,用for对比实现的: (() => {
function getIndex(arr) {
let index;
let n = Infinity;
for (let i = 0; i < arr.length; i++) {
const item = arr[i];
if (item > 0 && item<n) {
n = item;
index = i;
}
}
return index;
}
let arr = [10, 21, 0, -7, 35, 7, 9, 23, 18]
console.log(getIndex(arr));
})(); |
function getIndex(arr){
let index = null
index = arr.reduce((temp, v, i, arr)=>{
if(v > 0 && v < arr[temp]){
return i
}else{
return temp
}
}, 0)
return index
} |
// 不知道有没有重复的元素, 那就直接取了最前出现的, 时间复杂度O(n), 空间复杂度O(1)
function getIndex(arr){
let index = -1;
let min = Infinity
for(let i = 0; i < arr.length; i++){
if(arr[i] > 0 && arr[i] < min) {
min = arr[i]
index = i
}
}
return index;
}
let test = [
[[10,21,0,-7,35,7,9,23,18], 5],
[[10,21,0,-7,35,7,9,23,1], 8],
[[1,21,0,-7,35,7,9,23,1], 0],
[[], -1]
]
for(let i = 0; i < test.length; i++){
let [arr, n] = test[i]
console.log(getIndex(arr) === n)
} 其实就看怎么理解最精简, 我认为是时间空间复杂度小的, |
function getIndex(arr){
const indexMap = arr.reduce((map,item,index) => { map[item] = index ; return map;}, {});
return indexMap[arr.filter(i => i>0).sort(a, b => a - b)[0]] || -1;
} |
function getIndex(arr) {
const min = Math.min(...(arr.filter(i => i > 0)));
const index = arr.indexOf(min);
return index + ',' + min;
}
const arr = [10,21,0,-7,35,7,9,23,18];
console.log(getIndex(arr)); |
最精炼的代码,没说是最小的复杂度啊
|
赞同👍 |
const minIndex = (arr) => arr.reduce((num, v, i) => v > 0 && v < arr[num] ? i : num,0) |
// 方法一
function getIndex(arr){
let index = null;
let min;
for(let i = 0, l = arr.length; i < l; i++) {
if(arr[i] < min && arr[i] > 0 || !min && arr[i] > 0) {
min = arr[i];
index = i;
}
}
return index;
}
//方法二
function getIndex(arr){
return arr.indexOf(Math.min(...arr.filter(v => v > 0)));
} |
function getIndex(arr) {
return arr.reduce((v, x, i, ori) => x > 0 ? (x < ori[(v === -1 ? 0 : v)] ? i : v) : v, -1);
} |
这是来自QQ邮箱的假期自动回复邮件。
您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。
|
function findMin(arr){ |
这是来自QQ邮箱的假期自动回复邮件。
您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。
|
首末尾递归const getIndex = (arr, l=0, r = arr.length-1) =>
(l>=r)?l:(arr[l]>0 && (arr[r]<=0 || arr[l]<arr[r]))?getIndex(arr, l, r-1):getIndex(arr, l+1, r)
getIndex([10,21,0,-7,35,7,9,23,18]) 非递归(用于理解上面的递归)const list = [10,21,0,-7,35,7,9,23,18];
let index = -1;
let value = Number.MAX_SAFE_INTEGER;
let leftIndex = 0;
let rightIndex = list.length - 1;
while(leftIndex <= rightIndex) {
const leftValue = list[leftIndex];
if(leftValue<=0) {
leftIndex++;
continue;
}
const rightValue = list[rightIndex];
if(rightValue<=0) {
rightIndex--;
continue;
}
if(leftValue<rightValue) {
rightIndex--;
} else {
leftIndex++;
}
}
console.log(leftIndex); |
function getIndex (arr) {
|
function getSmallestPositiveInteger(nums) {
let ret = []; // tuple of 2D.
for (let i = 0; i < nums.length; i++) {
if((!ret[0] && !ret[1]) || (nums[i] < ret[0] && nums[i] > 0)) {
ret = [nums[i], i];
}
}
return ret;
} |
|
function getIndex(arr) { |
每次记录最小数角标, 数字小于0跳过, 否则与上次角标的数字比较,返回较小的角标 const getMinIndex = (e) => { |
function getIndex(arr) {
let index = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] > 0 && arr[i] < arr[index]) {
index = i;
}
}
return index;
} |
我补充一个蠢方法🤡 // 获取大于0的值的索引
function getIndex(arr) {
let index, temp = Infinity;
arr.forEach((item, i) => {
if (item > 0 && item < temp) {
index = i;
temp = item;
}
});
return index;
} |
|
|
function getIndex(arr) {
let index = null;
for (let i = 0, len = arr.length, min = Infinity; i < len; i++) {
const item = arr[i];
item > 0 && item < min && ([min, index] = [item, i]);
}
return index;
}
console.log(getIndex([-1, -2]));
console.log(getIndex([0, 0]));
console.log(getIndex([-1, 0, 1]));
console.log(getIndex([10, 21, 0, -7, 35, 7, 9, 23, 18]));
console.log(getIndex([0, 10, 21, 0, -7, 35, 7, 9, 23, 18]));
console.log(getIndex([6, 4, 1, 2, 3, 4, 5])); |
这是来自QQ邮箱的假期自动回复邮件。
您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。
|
function findMinIndex(arr) { |
|
|
|
export function getIndex(arr) { |
这是来自QQ邮箱的假期自动回复邮件。
您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。
|
const getMinIndex = (arr) => { const isMinValue = (item) => { arr.map((item, idx) => { return [minIndex, arr[minIndex]]; |
map改为foreach |
function getMinIndex(arr) { |
这是来自QQ邮箱的假期自动回复邮件。
您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。
|
const minIndex = (arr) => arr.reduce((num, v, i) => v > 0 && v < (arr[num] || Infinity) ? i : num, -1) 默认索引-1,再判断下arr[num]的值比较对 |
这是来自QQ邮箱的假期自动回复邮件。
您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。
|
排序 + indexOf |
例如:
[10,21,0,-7,35,7,9,23,18]
输出5, 7最小The text was updated successfully, but these errors were encountered: