列表

详情


FED49. 查找元素位置

描述

在数组 arr 中,查找值与 item 相等的元素出现的所有位置

示例1

输入:

['a','b','c','d','e','f','a','b','c'] 'a'

输出:

[0, 6]

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 113ms, 内存消耗: 38004KB, 提交时间: 2021-10-17

{"css":"","js":"function findAllOccurrences(arr, target) {\n   let newArr = []\n  arr.forEach((item, index) => {\n      if(item ===target) {\n          newArr.push(index)\n      }\n  })\n    return newArr\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 844ms, 内存消耗: 77772KB, 提交时间: 2021-03-15

{"css":"","js":"function findAllOccurrences(arr, target) {\n    var list = [];\n    arr.forEach(function(value,index,array){\n        (value == target) ? list.push(index) : 0\n    })\n    return list;\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 844ms, 内存消耗: 77772KB, 提交时间: 2020-12-24

{"css":"","js":"function findAllOccurrences(arr, target) {\n    let arr2 = []\n        index = arr.lastIndexOf(target)\n    while(index > -1) {\n        arr2.push(index)\n        arr.splice(index)\n        index = arr.lastIndexOf(target)      \n    }\n    return arr2\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 844ms, 内存消耗: 77772KB, 提交时间: 2020-11-23

{"css":"","js":"function findAllOccurrences(arr, target) {\nlet result=[]\nfor(key in arr){\n    if(arr[key]===target){\n        result.push(key)\n    }\n}\n    return result\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 844ms, 内存消耗: 77840KB, 提交时间: 2020-12-28

{"css":"","js":"function findAllOccurrences(arr, target) {\n\n      var list =[];\n   for(var i =0;i<arr.length;i++){\n       if(arr[i].indexOf(target) > -1){\n            list.push(i);\n       }\n     \n   }\n    \n    \n    return list;\n}","html":"","libs":[]}

上一题