翻译:疯狂的技术宅
原文: https://medium.freecodecamp.o...
本文首发微信公众号:前端先锋
欢迎关注,天天都给你推送新鲜的前端技术文章javascript
编写算法时,排序是一个很是重要的概念。它有各类各样的种类:冒泡排序、希尔排序、分块块排序,梳排序,鸡尾酒排序,侏儒排序 —— 这些可不是我瞎编的!前端
这个算法题可以让咱们一睹精彩的世界。咱们必须对数字数组进行升序排序,并找出给定数字在该数组中的位置。java
将值(第二个参数)插入到数组(第一个参数)中,并返回其在排序后的数组中的最低索引。返回的值应该是一个数字。
例如getIndexToIns([1,2,3,4], 1.5)
应该返回1
,由于1.5
大于1
(索引0),但小于2
(索引1)。
一样,getIndexToIns([20,3,5], 19)
应该返回2
,由于数组排序后应该是[3,5,20]
,19
小于20
(索引2)且大于5
(索引1)。
function getIndexToIns(arr, num) { return num; } getIndexToIns([40, 60], 50);
本算法题原题程序员
getIndexToIns([10, 20, 30, 40, 50], 35)
应该返回一个数字 3
。getIndexToIns([10, 20, 30, 40, 50], 30)
应该返回一个数字 2
.getIndexToIns([40, 60], 50)
应该返回一个数字 1
.getIndexToIns([3, 10, 5], 3)
应该返回一个数字 0
.getIndexToIns([5, 3, 20, 3], 5)
应该返回一个数字 2
.getIndexToIns([2, 20, 10], 19)
应该返回一个数字 2
.getIndexToIns([2, 5, 10], 15)
应该返回一个数字 3
.getIndexToIns([], 1)
应该返回一个数字 0
..sort()
,. indexOf()
理解问题:有两个输入:一个数组和一个数字。咱们的目标是将输入的数字在输入数组后中排序后,再返回它的索引。
示例/测试用例:咱们不知道输入的数组是以哪一种方式排序的,可是提供的测试用例清楚地代表,输入的数组应该从小到大进行排序。面试
请注意,在最后一个测试用例中存在边界问题,其中输入数组是一个空数组。算法
数据结构:因为咱们最终将会返回索引,所以应该坚持使用数组。segmentfault
咱们将会用一个名为 .indexOf()
的方法:数组
.indexOf()
返回元素在数组中出现的第一个索引,若是元素根本不存在则返回 -1
。例如:微信
let food = ['pizza', 'ice cream', 'chips', 'hot dog', 'cake'] food.indexOf('chips') // returns 2 food.indexOf('spaghetti') // returns -1
咱们将使用 .concat()
而不是 .push()
。为何呢?由于当使用 .push()
向数组添加元素时,它会返回新数组的长度。而使用 .concat()
向数组添加元素时,它会返回新数组自己。例如:数据结构
let array = [4, 10, 20, 37, 45] array.push(98) // returns 6 array.concat(98) // returns [4, 10, 20, 37, 45, 98]
算法:
num
插入 arr
。arr
进行升序排序。num
的索引。代码:
function getIndexToIns(arr, num) { // Insert num into arr, creating a new array. let newArray = arr.concat(num) // [40, 60].concat(50) // [40, 60, 50] // Sort the new array from least to greatest. newArray.sort((a, b) => a - b) // [40, 60, 50].sort((a, b) => a - b) // [40, 50, 60] // Return the index of num which is now // in the correct place in the new array. return newArray.indexOf(num); // return [40, 50, 60].indexOf(50) // 1 } getIndexToIns([40, 60], 50);
去掉局部变量和注释后的代码:
function getIndexToIns(arr, num) { return arr.concat(num).sort((a, b) => a - b).indexOf(num); } getIndexToIns([40, 60], 50);
.sort()
,.findIndex()
理解问题:有两个输入:一个数组和一个数字。咱们的目标是将输入的数字在输入数组后中排序后,再返回它的索引。
示例/测试用例:咱们不知道输入的数组是以哪一种方式排序的,可是提供的测试用例清楚地代表,输入的数组应该从小到大进行排序。
这个解决方案须要考虑两个边界状况:
0
,由于 num
将是该数组中的惟一元素,因此它在索引为 0
的位置。num
的位置处于升序排序后的 arr
的末尾,那么咱们须要返回 arr
的长度。数据结构:因为咱们最终将会返回索引,所以应该坚持使用数组。
让咱们看看.findIndex()
并了解它将如何帮助解决这一挑战:
.findIndex()
返回数组中第一个知足条件的元素索引。不然它将返回 -1
,这表示没有元素经过测试。例如:
let numbers = [3, 17, 94, 15, 20] numbers.findIndex((currentNum) => currentNum % 2 == 0) // returns 2 numbers.findIndex((currentNum) => currentNum > 100) // returns -1
这对咱们颇有用,由于咱们能够用 .findIndex()
将输入 num
与输入 arr
中的每一个数字进行比较,并找出它从最小到最大的顺序。
算法:
arr
是一个空数组,则返回 0
。num
处于排序后数组的末尾,则返回 arr
的长度。num
。代码:
function getIndexToIns(arr, num) { // Sort arr from least to greatest. let sortedArray = arr.sort((a, b) => a - b) // [40, 60].sort((a, b) => a - b) // [40, 60] // Compare num to each number in sortedArray // and find the index where num is less than or equal to // a number in sortedArray. let index = sortedArray.findIndex((currentNum) => num <= currentNum) // [40, 60].findIndex(40 => 50 <= 40) --> falsy // [40, 60].findIndex(60 => 50 <= 60) --> truthy // returns 1 because num would fit like so [40, 50, 60] // Return the correct index of num. // If num belongs at the end of sortedArray or if arr is empty // return the length of arr. return index === -1 ? arr.length : index } getIndexToIns([40, 60], 50);
去掉局部变量和注释的代码:
function getIndexToIns(arr, num) { let index = arr.sort((a, b) => a - b).findIndex((currentNum) => num <= currentNum) return index === -1 ? arr.length : index } getIndexToIns([40, 60], 50);
若是你有其余解决方案或建议,请在评论中分享!