This file contains 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
var crypto = require('crypto'); | |
var aesutil = module.exports = {}; | |
/** | |
* aes加密 | |
* @param data 待加密内容 | |
* @param key 必须为32位私钥 | |
* @returns {string} |
This file contains 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
//(1)在数据集之中,选择一个元素作为"基准"(pivot)。 | |
//(2)所有小于"基准"的元素,都移到"基准"的左边;所有大于"基准"的元素,都移到"基准"的右边。 | |
//(3)对"基准"左边和右边的两个子集,不断重复第一步和第二步,直到所有子集只剩下一个元素为止。 | |
function quickSort(arr) { | |
if (arr.length <= 1) { return arr; } | |
const pivotIndex = Math.floor(arr.length / 2); | |
const pivot = arr.splice(pivotIndex, 1)[0]; | |
const left = []; | |
const right = []; | |
for (var i = 0; i < arr.length; i++){ |
This file contains 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
// 插入排序(insertion sort)它将数组分成“已排序”和“未排序”两部分,一开始的时候,“已排序”的部分只有一个元素, | |
// 然后将它后面一个元素从“未排序”部分插入“已排序”部分,从而“已排序”部分增加一个元素,“未排序”部分减少一个元素。以此类推,完成全部排序。 | |
// 递归实现 | |
function insertionSort(arr, newArr=[]) { | |
const first = arr[0]; | |
if(newArr.length === 0){ | |
arr.shift(); | |
newArr.push(first); | |
} else { |
This file contains 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
// 选择排序(Selection Sort)与冒泡排序类似,也是依次对相邻的数进行两两比较。不同之处在于,它不是每比较一次就调换位置,而是一轮比较完毕,找到最大值(或最小值)之后,将其放在正确的位置,其他数的位置不变。 | |
//以对数组[3, 2, 4, 5, 1] 进行从小到大排序为例,步骤如下: | |
//假定第一位的“3”是最小值。 | |
//最小值“3”与第二位的“2”进行比较,2小于3,所以新的最小值是第二位的“2”。 | |
//最小值“2”与第三位的“4”进行比较,2小于4,最小值不变。 | |
//最小值“2”与第四位的“5”进行比较,2小于5,最小值不变。 | |
//最小值“2”与第五位的“1”进行比较,1小于2,所以新的最小值是第五位的“1”。 | |
//第五位的“1”与第一位的“3”互换位置,数组变为[1, 2, 4, 5, 3]。 | |
// 这一轮比较结束后,最小值“1”已经排到正确的位置了,然后对剩下的[2, 4, 5, 3]重复上面的过程。每一轮排序都会将该轮的最小值排到正确的位置,直至剩下最后一个位置,所有排序结束。 |
This file contains 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 bubbleSort(arr, newArr = []) { | |
for(let i = 1; i < arr.length; i++) { | |
if(arr[i-1] > arr[i]){ | |
[arr[i-1], arr[i]] = [arr[i], arr[i-1]]; | |
} | |
} |
This file contains 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(obj){ | |
//判断是否是简单数据类型, | |
if(typeof obj == "object"){ | |
//复杂数据类型 | |
var result = obj.constructor == Array ? [] : {}; | |
for(let i in obj){ | |
result[i] = typeof obj[i] == "object" ? deepCopy(obj[i]) : obj[i]; | |
} | |
}else { | |
//简单数据类型 直接 == 赋值 |
This file contains 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 instanceOf(A, B) { | |
let proto = A.__proto__; | |
const prototype = B.prototype; | |
while(true) { | |
if(proto === null) return false; | |
if(proto === prototype) return true; | |
proto = proto.__proto__; | |
} | |
} |
This file contains 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
'use strict'; | |
const fs = require('fs'); | |
/** | |
* tailf | |
* | |
* @param {String} filename 文件名 | |
* @param {Number} delay 读取不到内容时等待的时间,ms | |
* @param {Function} onError 操作出错时的回调函数,onError(err) |
This file contains 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 singleNumber(nums) { | |
for (let i = 1; i < nums.length; i++) { | |
nums[0] ^= nums[i]; | |
} | |
return nums[0]; | |
}; | |
console.log(singleNumber([2, 2, 1, 4, 4, 5, 5, 1, 8])); | |
// 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。 |
This file contains 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 week2Date(week_param = 1, year_param = new Date().getFullYear(), first_day_of_week = 1) { | |
/** | |
* @param {number} week_param - 第几周,默认是第一周 | |
* @param {number} year_param - 第几年,默认是今年 | |
* @param {number} first_day_of_week - 一周从星期几开始,取值范围是0~6, 默认是周一开始 | |
* @returns {date} start_date_obj - 这一周开始时间的Date实例 | |
*/ | |
const days_of_a_week = 7; // 一周的总天数 | |
const start_date_obj = new Date(year_param, 0, 1); | |
const start_date_day = start_date_obj.getDay(); |
NewerOlder