You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
### const comfiles = (arr, val) => {
let sta = -1, end = -1;
let obj = {}
arr.forEach((item, index) => {
if (item === val) {
if (obj[item]) {
end = index
} else {
obj[item] = index
sta = index
}
}
})
return [sta, end]
}
Activity
Cxy56 commentedon Jul 17, 2020
sisterAn commentedon Jul 19, 2020
解答一:findIndex、lastIndexOf
findIndex() 方法返回数组中满足提供的测试函数的第一个元素的索引。若没有找到对应元素则返回-1。
lastIndexOf() 方法返回指定元素(也即有效的 JavaScript 值或变量)在数组中的最后一个的索引,如果不存在则返回 -1。
解答二:二分查找
复杂度分析:
Airfald commentedon Sep 23, 2020
嗯,这算法可以, 但是在真正项目中我愿意用 findIndex , findLastIndex
XW666 commentedon Apr 14, 2021
JYone223 commentedon Apr 19, 2021
简化的二分查找
KylinLee commentedon Apr 19, 2021
二分查找找到后,双指针同时向前向后查找。