随笔分类 -  算法

摘要:const arr1 = [1,4,5,8,12,16,18] function binarySearch(arr, num) { let len = arr.length let leftIndex = 0 let rightIndex = len - 1 while(leftIndex <= r 阅读全文
posted @ 2020-08-18 21:29 毛小星 阅读(635) 评论(0) 推荐(1)
摘要:深度优先搜索和广度优先搜索是比较常见的算法,今天我们用js来实现以下 首先我们创建一下数据 const tree = { key: '第一层-1', children: [ { key: '第二层-1-1', children: [ { key: '第三层-1-1', children: [], } 阅读全文
posted @ 2020-08-10 21:45 毛小星 阅读(788) 评论(0) 推荐(0)
摘要:你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 第一种方法 var twoSum = funct 阅读全文
posted @ 2020-05-31 22:15 毛小星 阅读(965) 评论(0) 推荐(0)
摘要:记录一些数组去重的方法,如果有错误的地方,还望指正 1.利用es6中的Set()去重特性 const arr = [1,2,3,6,8,2,9,5,6,4,9] function uniq(arr) { return [...new Set(arr)] } const resultArr1 = un 阅读全文
posted @ 2020-05-31 22:05 毛小星 阅读(133) 评论(0) 推荐(0)