基础算法练习及相关技巧
2017-10-02 本文已影响6人
faremax
本文包括js学习中简单功能的算法
包括对js以及DOM和BOM的研究过程中一些有意思的代码实现
本文还包括公司面试相关算法问题的代码段,但不会指出是哪个公司出的题
数据结构及基本排序、查找算法
这个部分内容比较多,请查看一下博客:
递归实现斐波那契数列
function reFib(n){
if (n < 2) return n;
else return reFib(n - 1) + reFib(n - 2);
}
动态规划实现斐波那契数列
function dynFib(n){
var val = [];
for(var i = 0; i <= n; i++)
val.push(0);
if (n == 1 || n == 2) return 1;
else{
val[1] = 1;
val[2] = 2;
for (var i = 3; i <= n; ++i)
val[i] = val[i - 1] + val[i - 2];
return val[n - 1];
}
}
迭代实现斐波那契数列
function iterFib(n){ //得到第n个数列值
var a = 1, b = 1, temp;
for(var i = 2; i <= n; i++){
temp = b;
b = a + b;
a = temp;
}
return a;
}
迭代实现斐波那契数列 ES6
function* fibs(){
let a = 1;
let b = 1;
while(1){
yield a;
[a, b] = [a, a + b];
}
}
var [first, second, third, forth, fifth, sixth] = fibs();
数组元素去重复
function unique(arr) {
return arr.filter(function(item, index){
return arr.indexOf(item) === index;
});
} //除去所以 undefined
//以下是 ES6 方法
function unique(arr){
return Array.from(new Set(arr));
} //保留数组中的 undefined
Fisher Yates Shuffle
Array.prototype.shuffle = function(){
var len = this.length;
for(var i = len - 1; i > 0; i--){
var j = Math.round(Math.random() * i);
// debugger;
[this[i],this[j]] = [this[j],this[i]];
}
return this;
}
查找2个字符串的最长公共子串
function lcs(word1, word2){
var max = 0;
var index = 0;
var lcsarr = [];
var str = "";
for(var i = 0; i <= word1.length + 1; ++i){
lcsarr[i] = [];
for(var j = 0; j <= word2.length + 1; ++i)
lcsarr[i][j] = 0;
}
for(var i = 1; i <= word1.length; ++i){
for(var j = 1; j <= word2.length; ++j){
if(word1[i - 1] === word2[j - 1])
lcsarr[i][j] = lcsarr[i - 1][j - 1] + 1;
if(max < lcsarr[i][j]){
max = lcsarr[i][j];
index = i;
}
}
}
if(max === 0)
return "";
else {
for(var i = index - max; i < index; ++i)
str += word1[i];
return str;
}
}
背包问题 递归
/**
* [knapsack description]
* @param number capacity 背包容量(最大承重)
* @param array weights 背包内物品重量
* @param array value 物品对应的价值
* @param number n 物品数量
* @return number 求的的最大价值
*/
function knapsack(capacity, weights, value, n){
if(n === 0 || capacity === 0) return 0;
if(weights[n -1] > capacity) return knapsack(capacity, weights, value, n - 1);
else return Math.max(value[n - 1] + knapsack(capacity - weights[n - 1], weights, value, n - 1), knapsack(capacity, weights, value, n - 1));
}
背包问题 动态规划
/**
* [knapsack description]
* @param number capacity 背包容量(最大承重)
* @param array weights 背包内物品重量
* @param array value 物品对应的价值
* @param number n 物品数量
* @return number 求的的最大价值
*/
function dKnapsack(capacity, weights, value, n){
var K = []; //价格矩阵
for(var i = 0; i <= n; i++)
K[i] = [];
for(var i = 0; i <= n; i++){
for(var w = 0; w <= capacity; w++){
if(i == 0 || w == 0)
K[i][w] = 0;
else if(weights[i - 1] <= w)
K[i][w] = Math.max(value[i - 1] + K[i - 1][w - weights[i - 1]], K[i - 1][w]);
else
K[i][w] = K[i - 1][w];
console.log(K[i][w] + " ");
}
console.log();
}
return K[n][capacity];
}
背包问题 贪心法
/**
* [knapsack description]
* @param number capacity 背包容量(最大承重)
* @param array weights 背包内物品重量
* @param array value 物品对应的价值
* @return number 求的的最大价值
*/
function dKnapsack(capacity, weights, values){
var load = 0;
var i = 0;
var w = 0;
while(load < capacity && i < 4){
if(weight[i] <= (capacity - load)){
w += value[i];
load += weight[i];
} else {
var r = (capacity - load) / weight[i];
w += r * value[i];
load += weights[i];
}
++i;
}
return w;
}
找零问题 贪心法
function makeChange(origAmt, coins){
var remainAmt = 0;
if(origAmt % .25 < origAmt){
coins[3] = parseInt(origAmt / .25);
remainAmt = origAmt % .25;
origAmt = remainAmt;
}
if(origAmt % .1 < origAmt){
coins[2] = parseInt(origAmt / .1);
remainAmt = origAmt % .1;
origAmt = remainAmt;
}
if(origAmt % .05 < origAmt){
coins[1] = parseInt(origAmt / .05);
remainAmt = origAmt % .05;
origAmt = remainAmt;
}
coins[0] = parseInt(origAmt / .01);
}
function showChange(coins){
if(coins[3] > 0) console.log("25美分数量- " + coins[3] + " - " + coins[3] * .25);
if(coins[2] > 0) console.log("10美分数量- " + coins[2] + " - " + coins[2] * .1);
if(coins[1] > 0) console.log("5美分数量- " + coins[1] + " - " + coins[1] * .05);
if(coins[0] > 0) console.log("1美分数量- " + coins[0] + " - " + coins[0] * .01);
}
最大公约数 辗转相除法
function gcd(a, b){
return b === 0 ? a : gcd(b, a%b);
}
最大公约数 更相减损术
function gcd(a, b){
return a === b ? a : a < b ? gcd(a, b-a) : gcd(b, a-b);
}
hanoi塔 递归
function re_hanoi(n, a, b, c){
if(n !== 0){
re_hanoi(n - 1, a, c, b);
console.log(a + " => " + b + "\n");
re_hanoi(n - 1, c, b, a);
}
}
hanoi塔 非递归
function hanoi(n){ //0 1 2 共3个塔
for(var t = 1; t < 1 << n; t++){
var first, second;
switch((t & t - 1) % 3){
case 0: first = 'a'; break;
case 1: first = 'b'; break;
case 2: first = 'c'; break;
default: break;
}
switch(((t | t - 1) + 1) % 3){
case 0: second = 'a'; break;
case 1: second = 'b'; break;
case 2: second = 'c'; break;
default: break;
}
console.log(first + " => " + second + "\n")
}
}
查找n以内的最大质数(埃拉托色尼法)
function maxPrime(n){
var a = new Array(n + 1);
var max = Math.floor(Math.sqrt(n));
var p = 2;
while(p <= max){
for(var i = 2 * p; i <= n; i += p)
a[i] = 1;
while(a[++p]) /* empty */;
}
while(a[n]) n--;
return n;
}
身份证号码末尾校验码
/**
* [indentify description]
* @param String str 身份证号码
* @return Boolean 是否合法身份证号
*/
function indentify(str)
{
var weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
var check = ["1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"];
if(str.length !== 18) {
alert("input error");
str="";
} else {
var substr = str.slice(0, 17);
var last = str.slice(-1);
var S = 0;
for(var i = 0; i < 17; i++) {
S += substr[i] * weight[i];
}
S = S % 11;
if(check[S] === last.toUpperCase()) {
return true;
} else {
str="";
return false;
}
}
}
青蛙跳台阶 动态规划
一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
function jumpFloor(number){
if(number < 1){
return 0;
}
if(number === 1){
return 1;
}
if(number === 2){
return 2;
}
var temp = 0, a = 1, b = 2;
for(var i = 3; i <= number; i++){
temp = a + b;
a = b;
b = temp;
}
return temp;
}