This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 在指定范围内获取随机整数 | |
* @param {Number} min - 最小值 | |
* @param {Number} max - 最大值 | |
* @return {Number} | |
*/ | |
function getRandomIntInRange (min, max) { | |
return Math.floor(Math.random() * (max - min + 1) + min); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 斐波那契数列 | |
* of ES6 | |
*/ | |
function fibonacci () { | |
let fn1 = 0; | |
let fn2 = 1; | |
let next = reset => { | |
if (reset) { | |
fn1 = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 二分查找 | |
* @param {Array} array - 有序数组 | |
* @param {Number} value - 查找目标值 | |
*/ | |
function binarySearch (array, value) { | |
let index = 0; | |
let left = 0; | |
let right = array.length - 1; | |
while (left < right) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function lazyMan (name) { | |
let man = { name }; | |
let tasks = []; | |
let excute = () => { | |
let task = tasks.shift(); | |
task && task(); | |
}; | |
man.talk = () => { | |
tasks.push(() => { | |
console.log(`Hi! This is ${name}!`); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 快速排序 | |
* @param {Array} array - 乱序数组 | |
*/ | |
function quickSort (array) { | |
if (0 === array.length) { return array; } | |
let key = array.shift(); | |
let lessers = []; | |
let greaters = []; | |
for (let i = 0, item = null; item = array[i]; i++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 对象深拷贝 | |
*/ | |
function deepCopy (object, pairs = []) { | |
let copy = {}; | |
pairs.push([object, copy]); | |
for (let key in object) { | |
if (object.hasOwnProperty(key)) { | |
let value = object[key]; | |
if (Array.isArray(value)) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 查找出现频率最高项 | |
* @param {Array} array | |
*/ | |
function findMostFreq (array) { | |
let freqs = {}; | |
let length = array.length; | |
let result = { item: undefined, freq: 0 }; | |
for (let i = 0; i < length; i++) { | |
let item = array[i]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Promises/A+ | |
* @see http://www.ituring.com.cn/article/66566 | |
* @param {Function} executor | |
*/ | |
function MyPromise (executor) { | |
let PromiseStatus = "pending"; | |
let PromiseValue = undefined; | |
let PromiseTasks = []; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 判断质数 | |
* @param {Nubmer} nubmer | |
* @return {Boolean} | |
*/ | |
function isPrime (number) { | |
let isNumber = "number" === typeof number; | |
let isInteger = isNumber && 0 === number % 1; | |
let isNatural = isInteger && -1 < number; | |
if (!isNatural || 2 > number) { return false; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 回形矩阵 | |
* @param {Number} n - 矩阵尺寸 | |
* @return {String} | |
*/ | |
function spiralMatrix (n) { | |
let matrix = []; | |
let a = n ** 2; | |
let p = a.toString().length; | |
for (let x = 0; x < n; x++) { |
OlderNewer