FCC编程题之中级算法篇(下)
介绍
本篇是"FCC编程题之中级算法篇"系列的最后一篇
这期完结后,下期开始写高级算法,每篇一题
目录
- 1. Smallest Common Multiple
- 2. Finders Keepers
- 3. Drop it
- 4. Steamroller
- 5. Binary Agents
- 6. Everything Be Tru
- 7. Arguments Optional
1. Smallest Common Multiple
Find the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by all sequential numbers in the range between these parameters.
The range will be an array of two numbers that will not necessarily be in numerical order.
e.g. for 1 and 3 - find the smallest common multiple of both 1 and 3 that is evenly divisible by all numbers between 1 and 3.
Here are some helpful links:
- Smallest Common Multiple
寻找给定两数及其中间的所有数的最小公倍数
思路
-
最小公倍数一定能整除所有的数,此处假设该数组中的最大的数即为最小公倍数。
-
用假设的最小公倍数除以给定范围中的所有的数,如果都能整除,说明该假设的数即为最小公倍数。
-
如果有不能整除的数,则将假设的数加上数组中最大的数,在重复第二步。
function smallestCommons(arr) {
let [start, end] = arr[0] < arr[1] ? [arr[0], arr[1]] : [arr[1], arr[0]];
for (var i = start, com = end; i < end; i++) {
if (com % i !== 0) {
i = start - 1;
com += end;
}
}
return com;
}
2. Finders Keepers
Create a function that looks through an array (first argument) and returns the first element in the array that passes a truth test (second argument).
Here are some helpful links:
- Array.prototype.filter()
方法 1
用帮助栏的filter()
方法, filter()
方法返回一个数组,判断数组的长度,如果不为零,则返回数组第一个元素,否则返回undefined
function findElement(arr, func) {
let arr1 = arr.filter((val) => {
return func(val);
});
return arr1.length === 0 ? undefined : arr1[0];
}
方法 2
用find()
方法,find()
方法接收一个测试函数作为参数,并返回满足测试函数的第一个元素,如果没有则返回undefined
function findElement(arr, func) {
return arr.find(func);
}
3. Drop it
Drop the elements of an array (first argument), starting from the front, until the predicate (second argument) returns true.
The second argument, func, is a function you'll use to test the first elements of the array to decide if you should drop it or not.
Return the rest of the array, otherwise return an empty array.
Here are some helpful links:
- Arguments object
- Array.prototype.shift()
- Array.prototype.slice()
思路
使用while()
循环对数组元素进行判断,没有通过测试函数的直接删除即可
function dropElements(arr, func) {
while (!func(arr[0])) {
arr.shift(0);
}
return arr;
}
4. Steamroller
Flatten a nested array. You must account for varying levels of nesting.
Here are some helpful links:
- Array.isArray()
遍历嵌套数组思路即可解题
Array.isArray()
方法接收一个参数,判断这个参数是否是数组
思路
- 创建一个递归函数,判断数组元素是否为数组,不是数组就返回其值,是数组则使用递归函数
function steamrollArray(arr) {
let convert = arr => arr.reduce((arr, val) => {
return arr.concat(Array.isArray(val) ? convert(val) : val);
}, []);
return convert(arr);
}
5. Binary Agents
Return an English translated sentence of the passed binary string.
The binary string will be space separated.
Here are some helpful links:
String.prototype.charCodeAt() String.fromCharCode()
将二进制数翻译为英文
思路
-
parseInt()
方法接收两个参数,第一个参数为字符串,第二个为基数 -
利用
fromCharCode()
方法返回Unicode值序列创建的字符串。 -
将字符串拼接在一起
function binaryAgent(str) {
return str.split(' ').map((val) => {
return String.fromCharCode(parseInt(val, 2));
}).join('');
}
6. Everything Be Tru
Check if the predicate (second argument) is truthy on all elements of a collection (first argument).
Remember, you can access object properties through either dot notation or [] notation.
根据测试用例可以看出所测属性的值也需为真才行
思路
-
利用
every()
方法对每个元素都调用测试函数判断一次 -
判断对象是否有指定属性,如有,则其值是否为真
function truthCheck(collection, pre) {
return collection.every((val) => {
return val.hasOwnProperty(pre) && !!val[pre];
});
}
7. Arguments Optional
Create a function that sums two arguments together. If only one argument is provided, then return a function that expects one argument and returns the sum.
For example, addTogether(2, 3) should return 5, and addTogether(2) should return a function.
Calling this returned function with a single argument will then return the sum:
var sumTwoAnd = addTogether(2);
sumTwoAnd(3) returns 5.
If either argument isn't a valid number, return undefined.
Here are some helpful links:
- Closures
- Arguments object
考察闭包
思路
利用isFinite()
方法判断元素是否为数字
function addTogether() {
let arr = Array.prototype.slice.call(arguments);
if (arr.length === 2) {
return arr.every(Number.isFinite) ? arr[0] + arr[1] : undefined;
} else {
return Number.isFinite(arr[0]) ? (val) => { return Number.isFinite(val) ? arr[0] + val : undefined; } : undefined;
}
}
欢迎大家去我的简书看看