第 161 题:用最精炼的代码实现数组非零非负最小值index
libin1991 opened this issue · 185 comments
例如:[10,21,0,-7,35,7,9,23,18]
输出5, 7最小
function getIndex(arr){
let index=null;
...
return index;
}
/**
* @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;
}
@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/
您访问下这个地址,这上面有这个demo,但是没讲这个!的意思,我想了解一下,麻烦了 @slogeor
const arr = [10, 21, 0, -7, 35, 7, 9, 23, 18]; function findMinimumIndex(arr) { return arr.findIndex(item => item === arr.filter(item => item > 0).sort((a, b) => a - b)[0]) } console.log(findMinimumIndex(arr))
这个时间复杂度也不低
@libin1991 楼主,我是否可以贡献一题,实现 JSON.stringify 方法
@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)
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;
}
输出答案都是错的!
getIndex([10,21,0,-7,35,7,9,23,18])
// 8
@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)
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; }
输出答案都是错的!
getIndex([10,21,0,-7,35,7,9,23,18]) // 8
调整了一下
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;
}
常规写法
function rankVoteMaxIndex(arr) { let itemIndex = 0; // 非0最小index let itemRank=0; // 非0最小值 let length=arr.length; if(!length){ return -1; } for(let i=0;i<length;i++){ let item=arr[i]; if(item<0) continue; if(!itemRank) { itemIndex = i; itemRank = item; } if(item < itemRank ){ itemIndex = i; itemRank = item; } } return itemIndex; }这个答案面试官给了65分(满分100)
rankVoteMaxIndex([-1] // 0
rankVoteMaxIndex([-1, 0, 1] // 0
<script>
function rankVoteMaxIndex(arr) {
let itemIndex = -1; // 非0最小index
let itemRank = -1; // 非0最小值
let length = arr.length;
if (!length) {
return itemIndex;
}
for (let i = 0; i < length; i++) {
let item = arr[i];
if (item > 0) {
if (itemRank < 0) {
itemIndex = i;
itemRank = item;
}
if (item < itemRank) {
itemIndex = i;
itemRank = item;
}
}
}
return itemIndex < 0 ? -1 : itemIndex;
}
// rankVoteMaxIndex([-1] // 0
// rankVoteMaxIndex([-1, 0, 1] // 0
console.log(rankVoteMaxIndex([-1]));
console.log(rankVoteMaxIndex([-1, 0, 1]));
console.log(rankVoteMaxIndex([10, 21, 0, -7, 35, 7, 9, 23, 18]))
console.log(rankVoteMaxIndex([0, 10, 21, 0, -7, 35, 7, 9, 23, 18]))
</script>
会不会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的数,然后排序,最小值就是非负非零的最小值,楼上的这个也算一个方法
const minIndex = (arr) => arr.reduce((num, v, i) => v > 0 && v < arr[num] ? i : num, -1)
初始值为-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用法相关参考链接:
https://aotu.io/notes/2016/04/14/js-reduce/index.html
function getIndex(arr) {
return arr.reduce((idx, cur, k) => idx = cur <= idx[1] && cur > 0 ? [k, cur] : idx, [-1, Number.MAX_SAFE_INTEGER])[0]
}
const minIndex = (arr) => arr.reduce((num, v, i) => v > 0 && v < arr[num] ? i : num, -1)
初始值为-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)); // 5reduce用法相关参考链接:
https://aotu.io/notes/2016/04/14/js-reduce/index.html
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));
最精炼的代码,没说是最小的复杂度啊
function getIndex(arr) {
let min=Math.min(...arr.filter(i=>i>0));
let index=arr.indexOf(min);
return [index, min].join(', ');
}
最精炼的代码,没说是最小的复杂度啊
function getIndex(arr) { let min=Math.min(...arr.filter(i=>i>0)); let index=arr.indexOf(min); return [index, min].join(', '); }
赞同👍
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);
}
function getIndex(arr){
let index=null;
let min = arr[0];
let len = arr.length;
for(let i = 0; i < len;i++){
if(arr[i] > 0){
if(min > arr[i]){
index = i;
min = arr[i];
}
}
}
return index;
}
逐个对比每次记录最小值以及下标
const minIndex = arr =>
arr.indexOf(Math.min(...arr.filter(val => val > 0)));
function getIndex(arr) {
return arr.reduce((pre, next, k) => {
if (next <=0) return -1
if (pre === -1) return k
return Math.min(arr[pre], next) === next ? k : pre
}, -1)
}
getIndex([10,21,0,-7,35,7,9,23,18]) // 5
const minIndex = (arr)=>{
let arr1= arr.filter(item=>item>0)
retutn arr1.length > 0 ? arr.findIndex(Math.min(...arr1)) : -1
}
minIndex([-1,0,0,0])
function getMin(arr) {
let min = 0;
let result = 0;
arr.forEach((item, index) => {
if (item > 0 && (!min || item < min)) {
min = item;
result = index;
}
});
return result;
}
一个for循环解决
// 方法一
this.minVal = this.arr[0];
this.result = 0;
for (let i = 0; i < this.arr.length; i++) {
let curVal = this.arr[i];
if (curVal > 0 && curVal < this.minVal) {
this.minVal = curVal;
this.result = i;
}
}
console.log(this.minVal,this.result, "val")
}`
function getIndex(arr) {
let index = null
index = arr.reduce(function (minI, item, i) {
return item > 0 && (item < arr[minI] || minI === null) ? i : minI
}, null)
return index
}
function getIndex(arr){
let i = 0;
let index;
let min;
while (i < arr.length) {
if (!min && arr[i] > 0) {
index = i;
}
if (min && arr[i] > 0 && arr[i] < min) {
index = i;
}
i++;
}
return index;
}
const minIndex = (arr) => arr.reduce((num, v, i) => v > 0 && v < arr[num] ? i : num, 0)
使用reduce可选参数要从0开始,-1的话会输出-1
const array = [0,-10,-5,1,9,5,1,4,7,10];
function getIndex(arr){
let minValue = arr.filter(item => item > 0).sort((a,b) => a -b)[0]
return arr.indexOf(minValue)
}
console.log(getIndex(array));
function getIndex1(arr){
return arr.findIndex(item => item === arr.filter(item => item > 0).sort((a,b) => a -b)[0]);
}
console.log(getIndex1(array));
function getIndex(arr){
let index=null;
let minNum = Math.min(...arr.filter(item => item > 0));
index = arr.findIndex(item => item === minNum);
return index;
}
function getIndex(arr) {
let min = undefined;
let index = -1;
arr.forEach((item, i) => {
if (item > 0 && ((min && item < min) || !min)) {
min = item;
index = i;
}
})
return index;
}
function getIndex(arr) {
let index = -1;
let min = Number.MAX_SAFE_INTEGER;
for (const i in arr) {
if (arr[i] < min && arr[i] > 0) {
min = arr[i];
index = i;
}
}
return index;
}
function getIndex(arr){
let minIndex = -1;
let min = Number.MAX_SAFE_INTEGER;
arr.forEach( (item, index) => {
if( item > 0 && item < min){
min = item;
minIndex = index;
}
})
return minIndex;
}
function getIndex(arr) {
let min = Math.min(...arr.filter(item => item > 0))
let index = arr.indexOf(min)
return ${index} ${min}
}
function getIndex(arr){
let res = Array.from(arr)
return res.indexOf(arr.filter((v, i) => v > 0).sort((a,b) => Number(a) - Number(b))[0]);
}
function getIndex(arr) {
let index = 0, mixEle = arr[0]
arr.forEach((ele, i) => {
if (ele > 0 && mixEle > ele) {
index = i
mixEle = ele
}
});
return index
}
这个只要遍历一遍就好了吧?
// 时间复杂度O(N)
function getIndex(arr) {
let index = -1;
arr.reduce((prev, cur, i) => {
if (cur > 0) {
const min = Math.min(prev, cur);
if (cur === min) {
index = i;
prev = cur;
}
}
return prev;
}, Infinity);
return index;
}
const res = getIndex([10, 21, 0, -7, 35, 7, 9, 23, 18]);
console.log(res); //5
题目有点不清楚,我理解的是,如果一个数组中全部都是<= 0的数,那返回-1,而且用reduce看起来很酷,但性能真的没有for循环好呀,是我对这个题目没有理解清楚吗?大家都用reduce做
function getIndex(arr){
if(arr.length <= 0) return -1
let index = -1;
for(let i = 0; i < arr.length; i++) {
if(index < 0) {
index = arr[i] > 0 ? i : index
continue;
}
if(arr[i] < arr[index] && arr[i] > 0) {
index = i
}
}
return index;
}
function getIndex(arr) { return arr.indexOf(Math.min.apply(null, arr.filter(item => item > 0))) }
let arr = [10, 21, 0, -7, 35, 7, 9, 23, 18]
let min = -1
let minItem = arr[0]
let len = arr.length
for (let i = 0; i < len; i++) {
if (arr[i] > 0 && arr[i] < minItem) {
min = i
minItem = arr[i]
}
}
console.log(min, minItem)
for循环的
function getIndex (arr) {
let index = -1
for (let i = 0; i < arr.length; i++) {
if (arr[i] > 0 && (index < 0 || arr[i] < arr[index])) {
index = i
}
}
return index
}
优雅的
function getIndex (arr) {
const _arr = arr.filter(val => val > 0)
const min = Math.min(..._arr)
const index = arr.indexOf(min)
return index
}
精(zhuang)简(bi)的
function getIndex (arr) {
return arr.reduce((index, val, i) => val <= 0 ? index : ((index < 0 || arr[i] < arr[index]) ? i : index), -1)
}
let obj = [10, 21, 0, -7, 35, 7, 9, 23, 18].reduce((result, item, index) => {
if (!result[item] && item > 0) {
result[item] = index;
}
return result
}, {})
let res = Object.keys(obj)[0]
console.info(res, obj[res])
function getIndex(arr) {
let index = null;
index = arr.findIndex(item => item === Math.min(...arr.filter(item => item > 0)))
return index;
}
一次遍历就行了,循环找到符合条件的替换下标返回,不符合还返回原本下标
function getIndex(arr){
return arr.reduce((acc, cur, idx)=> cur > 0 && cur < arr[acc] ? idx : acc, -1)
}
function getIndex(arr){
// 先找到第一个非符的数及其下标
let index = 0;
while(arr[index] <=0 && index < arr.length){
index++;
}
if(index >= arr.length){
return -1;
}
// 从第一个非负的数字开始遍历
let min = arr[index];
for(let i = index+1;i < arr.length;i++){
if(arr[i] > 0 && arr[i] <min){
min = arr[i];
index = i;
}
}
return index;
}
function getIndex(arr){
return arr.indexOf(Math.min.apply(null,[].concat(arr).filter(item=>item>0)))
}
function getMinIndex(arr) {
let min = Infinity
let minIndex
arr.forEach((v, i) => {
if (typeof v === 'number' && v > 0) {
if (v < min) {
min = v
minIndex = i
}
}
})
return minIndex
}
easy
function getIndex(arr){
let index=null;
let index_0 = arr[0] // 假设符合预期的值为第一个
for(let i = 0; i < arr.length; i++) {
if(arr[i] <= index_0 && arr[i] > 0) {
index_0 = arr[i]
index = i
}
}
return index;
}
题目考的是应该条件判断的使用: 如何用最精炼的条件判断来描述题目给出的逻辑
使用reduce
如果只要求找非0非负最小值
条件判断next < 0 || (prev > 0 && perv < next) ? prev : next
- 如果next < 0 则直接返回prev
- 如果next > 0, 继续判断prev; 如果prev < 0 直接返回next
- 如果上面两条都不满足 证明next和prev都大于0,最后再比较prev和next的大小
如果只找到满足条件的值,这么写即可;
如果数组内的值都是小于等于0的值,会默认返回第一个值(可以根据具体情景做调整)
function getIndex(arr) {
return arr.reduce((prev, next) => (next < 0 || (prev > 0 && prev < next) ? prev : next))
}
题目不光要求找到值,还要求找到下角标,用变量存一下下角标就行了
function getIndex(arr) {
let minIdx = -1;
let min = arr.reduce((prev, next, currIdx) => {
if (next < 0 || (prev > 0 && prev < next)) {
return prev;
} else {
minIdx = currIdx;
return next;
}
});
return minIdx > -1 ? [minIdx, min].join(",") : -1;
}
function getIndex(arr) {
let index = arr.filter(i=>i>0).sort((a,b)=>a-b)[0];
let value=arr.findIndex(i=> i ==value )
return [value, index ].join(",")
}
// 最小正数
function minRPlus(arr) {
const l = arr?.length;
let i = 0,
minIdx;
// 先找到最小非零、非负数
for (; i < l; i++) {
if (arr[i] > 0) {
minIdx = i;
break;
}
}
// 再找到最小数
for (; i < l; i++) {
if (arr[i] > 0 && arr[i] < arr[minIdx]) {
minIdx = i;
}
}
return minIdx;
}
function getIndex(arr) {
let index = null;
for (let i = 0; i < arr.length; i++) {
if(arr[i] > 0) {
if(arr[index] > arr[i] || !index) {
index = i
}
}
}
return index;
}
类似选择排序,只是不需要排序,而是在第一层遍历时增加逻辑判断。
function getIndex(arr){
let index=null;
index = arr.findIndex(item => item === Math.min.apply(null, arr.filter(item => item > 0)))
return index;
}
虽然代码有点多,但时间复杂度应该是最低的吧?
function getIndex(arr){
let index = -1;
if(!Array.isArray(arr) || arr.length==0) return index;
let target;
for(let i=0,len=arr.length; i<len; i++){
const value = arr[i];
if(typeof target==="undefined"){
target = value;
if(target>0){
index = i;
}
}else{
if(target>value && value>0){
target = value;
index = i;
}
}
}
return index;
}
const a = [10,21,0,-7,35,7,9,23,18];
console.log(getIndex(a))
const find05 = (arr) => {
let index = null, minval = arr[0];
for (let i = 0; i < arr.length; i++) {
let val = arr[i];
if (val < minval && val > 0) {
minval = val
index = i
}
}
return index
}
const arr = [10, 21, 0, -7, 35, 7, 9, 23, 18]
function getIndex(arr) {
let index = -1
arr.forEach((item, i) => {
if(item <= 0) {
index ++
}else if(item < arr[index]) {
index = i
}
})
return index
}
const index = getIndex(arr)
console.log(index, arr[index])
function getIndex(arr){
return arr.indexOf(Math.min(...arr.filter(item=>item>0)))
}
function getIndex(arr){
let index=null;
index = arr.indexOf( arr.filter(a => a > 0).sort((a, b ) => { return a - b })[0])
return index;
}
`
function getMin(arr,min = 0) {
for (let i=0; i<arr.length; i++) {
if (arr[i]>0 && min==0) min = i;
min = arr[i]>0 && arr[i]< arr[min] ? i : min
}
return min
}
`
var list = [10,21,0,-7,35,7,9,23,18];
function getIndex(arr) {
let index = null
let num = arr[0]
arr.forEach((item, index2) => {
if (num > 0 && arr[index2 + 1] > 0 && num > arr[index2 + 1]) {
num = arr[index2 + 1]
index = index2 + 1
}
})
return index
}
console.log('getIndex', getIndex(list))
function getMinIndex(list){
const result = [Infinity, -1];
for(let i = 0 , len = list.length ; i < len ; i++){
const temp = list[i]
if(temp > 0 && temp < result[0]){
result[0] = temp
result[1] = i
}
}
if(result[1] > -1){
return result[1];
}else {
return undefined;
}
}
时间复杂度 O(n)
function findNumIndex(arr) {
let result = [-1, Infinity];
arr.forEach(function (obj, index) {
if (obj > 0 && obj < result[1]) {
result = [index, obj];
}
});
return result;
}
这个最简练确实每个人理解都有不同
const min = arr.filter(v => v > 0).sort((a, b) => a - b)[0]
const index = arr.findIndex(v => v === min);
function findMinNumIndex(arr) {
if (!arr || !(arr || []).length) return -1
let min = { index: -1, data: Infinity }
arr.forEach((ele, idx) => {
if (ele > 0 && ele < min.data) {
min.data = ele
min.index = idx
}
})
return min.index
}
function getIndex(arr){
let indexs = arr.filter((item)=>item !== 0 && Math.sign(item) !== -1);
let a0 = indexs.sort((a,b)=> a-b)[0];
return arr.indexOf(a0);
}
function findMinNumIndex(arr){
let min = Number.MAX_SAFE_INTEGER, ind = -1;
arr.forEach((item,index) => {
if(typeof item === 'number' && item > 0 && item < min){
min = item;
ind = index;
}
});
return ind;
}
function getIndex(arr) {
const [, minIndex] = arr.reduce(
([min, minIndex], el, index) => {
if (el > 0 && (minIndex === -1 || min > el)) {
return [el, index];
}
return [min, minIndex];
},
[null, -1],
);
return minIndex;
}
function getIndex(arr) {
const [, minIndex] = arr.reduce(
([min, minIndex], el, index) => {
if (el > 0 && (minIndex === -1 || min > el)) {
return [el, index];
}
return [min, minIndex];
},
[null, -1],
);
return minIndex;
}
function getIndex(arr) {
let index = null;
arr.reduce((min, cur, internalIndex) => {
if (cur <= 0) return min;
if (cur < min) {
index = internalIndex;
return cur;
}
return min;
}, Infinity)
return index;
}
function getIndex(arr) {
let index = null
const obj = {}
arr.forEach((num, i) => {
if (num > 0) {
obj[num] = i
}
})
index = Object.values(obj)[0]
return index
}
// 尝试使用补码
function getIndex(arr){
return arr.reduce(
(prevIndex,curVal,curIndex) =>
~curVal < -1
&&
(!~prevIndex || arr[prevIndex] >= curVal)
?
curIndex
:
prevIndex,
-1)
}
function getIndex(arr) {
return arr.reduce((minIndex, value, index) => {
if (value > 0) {
if (minIndex === -1) {
minIndex = index;
} else if (value < arr[minIndex]) {
minIndex = index;
}
}
return minIndex;
}, -1);
}
按我自己理解方式试了一把,又是留下了没有技术的泪水一天
`
// 找出最小值 [10,21,0,-7,35,7,9,23,18]
const temp = [10, 21, 0, -7, 35, 7, 9, 23, 18];
function findMin(value) {
if (value.length === 0) return [-1, -1];
return value.reduce((cur, item, index) => {
console.log("item", item, cur);
if (index === 0) cur = [index, item];
if (item > 0 && item < cur[1]) {
cur[0] = index;
cur[1] = item;
}
return cur;
}, []);
}
findMin(temp);
`
按我自己理解方式试了一把,又是留下了没有技术的泪水一天
// 找出最小值 [10,21,0,-7,35,7,9,23,18]
const temp = [10, 21, 0, -7, 35, 7, 9, 23, 18];function findMin(value) { if (value.length === 0) return [-1, -1]; return value.reduce((cur, item, index) => { console.log("item", item, cur); if (index === 0) cur = [index, item]; if (item > 0 && item < cur[1]) { cur[0] = index; cur[1] = item; } return cur; }, []); } findMin(temp);
这样好像也行
function findMin2(value) {
if (value.length === 0) return false;
return value.reduce((cur, item, index) => item > 0 && item < value[cur] ? index : cur, value.length - 1);
}
例如:
[10,21,0,-7,35,7,9,23,18]
输出5, 7最小function getIndex(arr){ let index=null; arr.reduce((acc, cur, i) => { if (acc > cur && cur > 0) { index = i return cur } else { return acc } }) return index; }
function getIndex(arr){ return arr.indexOf(arr.reduce((acc, cur, index) => acc > cur && cur > 0 ? cur : acc)) }
// 例如:[10,21,0,-7,35,7,9,23,18] 输出 5, 7 最小
function getIndex(arr) {
let index = null
let prev = null
arr.forEach((item, currentIndex) => {
if (item > 0 && (!prev || item < prev)) {
prev = item
index = currentIndex
}
})
return index
}
const index = getIndex([10,21,0,-7,35,7,9,6,23,18])
console.log(index)
function demo(arr) {
let index = 0;
let min = arr[0];
for (let i = 1; i < arr.length - 1; i++) {
if (arr[i] > 0 && arr[i] < min) {
index = i;
min = arr[i];
}
}
return [index, min]
}
const arr = [10,21,0,-7,35,7,9,23,18];
function getIndex(arr){
return arr.reduce((prev, next, index) => (next>0 && next < prev[0]) ? [next, index] : prev ,[Infinity, -1]);
}
getIndex(arr)
这应该挺简洁的
先找出大于0的数,然后排序,最小值就是非负非零的最小值,楼上的这个也算一个方法
准确来说,第二把冒泡拿到最小的就行,不需要完整的排序,复杂度两者还是有点差距的。
function getIndex(arr) {
let min = -1;
return arr.reduce((res, item, idx) => {
if (item > 0 && (idx === 0 || item < min)) {
min = item;
return idx;
}
return res;
}, -1);
}
function getIndex(arr) {
let index = null;
const res = arr.reduce((obj, item, index) => {
if (obj.value) {
if (item > 0 && item < obj.value) {
return {value: item, index}
}
return obj;
} else {
if (item > 0) {
return {value: item, index}
}
return obj;
}
}, {})
index = res.index;
return index;
}
function getIndex(arr){
if(!arr || arr.length ===0) {return null}
let index=null;
arr.forEach((item,theIndex) =>{if((index === null || item < arr[index]) && item > 0) index = theIndex} )
return index;
}
时间复杂度N,空间复杂度1
function g(a) {
return a.findIndex(i => i === a.filter(i => i > 0).sort((a, b) => a - b)[0])
}
g([10, 21, 0, -7, 35, 7, 9, 23, 18])
function g(a) { return a.findIndex(i => i === a.filter(i => i > 0).sort((a, b) => a - b)[0]) } g([10, 21, 0, -7, 35, 7, 9, 23, 18])
.length
133个字符~~
function getIndex(arr) {
const newArr = [...arr].sort((a, b) => a - b).filter(x => x > 0)
return newArr.length ? arr.indexOf(newArr[0]) : null;
}
function getIndex(arr){
return arr.reduce((pre,cur,index,array)=>{
if(cur>0){
return cur>array[pre]? pre:index;
}
return pre;
},null)
}
function getIndex(arr){
let index=null;
for(let i = 0;i<arr.length;i++){
if(arr[i]>0 && (index === null||arr[i]<arr[index])){
index = i
}
}
return index;
}
很简单的题怎么被你们整的那么复杂
function getIndex(arr){
let maxValue = Infinity;
let index=null;
arr.forEach((element, ind) => {
if (element > 0 && element < maxValue) {
maxValue = element;
index = ind;
}
});
return index;
}
这样不就行了嘛
// 实现数组非零非负最小值index
const minIndex = arr.reduce((pre, cur, i) => cur > 0 ? (cur > arr[pre] ? pre : i) : pre);
Without reduce
function getIndex(arr) {
let index = null;
let m = Number.POSITIVE_INFINITY;
for(let i=0; i<arr.length; i++){
if (arr[i] < m && arr[i] > 0) {
index = i;
m = arr[i];
}
}
return index;
}
function posMin(arr) { let arr2 = arr.map(i => i + Math.abs(i)); let min = 0; return arr2.reduce((acc, cur, idx) => { return !cur ? acc : !min || cur < min ? (min = cur, idx) : acc; }, -1) }
没跑几个用例,写了图一乐
function getIndex(arr) {
let index = -1;
let prev = Infinity;
arr.forEach((item, idx) => {
if (item > 0 && item < prev) {
prev = item;
index = idx;
}
});
return index;
}