爱奇艺三道js
1. 排序
function qSort(a, arr){ var temp = arr.slice(0); var comArr = arr.sort(function(s,t){ return s-t; }); var times = 0; for (var i = 0; i < a; ++i){ if(temp[i] === comArr[i]){ ++times; } } return a-times; }
2.回文素数
function palindrome(num){ return num.toString() === num.toString().split('').reverse().join(''); } function isPrime(left,right){ var i,j; var arr = []; for(i = left; i < right; i++){ for(j=2; j < i; j++){ if(i%j === 0) { break; } } if(i <= j && i !=1){ arr.push(i); } } var len = arr.length; for (var item of arr){ if(!palindrome(item)){ --len; } } return len; }
3.字符串价值
function find (str, num){ var temp = str.split(''); var store = {}; for (var item of temp){ if (store[item] == undefined) store[item] = 1; else ++store[item]; } for (var i = 0;i < num; ++i){ var max = 0,flag; for (var key in store){ if (store[key] > max){ max = store[key]; flag = key; } } --store[flag]; } var res = 0; for (var anotherKey in store){ res += store[anotherKey]*store[anotherKey]; } return res; }
#爱奇艺#