Open VaJoy opened 8 years ago
function maxFiveDigits(num) {
var str = num + '',arr = [];
if(str.length < 5){
return
}else{
for( var i = 0; i < str.length - 4; i ++){
arr.push(str.substr(i,5))
}
}
return Math.max.apply(window, arr);
} maxFiveDigits(984574152479)
function maxFiveDigits(num) {
//TODO:完成该函数
num = '' + num;
var start = num.length - 5;
if (start < 0) throw new Error('Parameter illegal');
var last = +num.substring(start);
return start == 0 ? last : num.split('').reduceRight(function (pre, cur, index, array) {
return (index < start) ? (last = Math.max(num.substring(index, index+5), last)) : last;
});
}
var num = 92999999799999;
alert( maxFiveDigits(num) );
function maxFiveDigits(num){
num = parseInt(num);
var reg = /\d{5}/g;
if( !(isNaN(num)) && reg.test(num) ){
var i = 0;
reg.lastIndex = i;
var matched;
var arr = [];
while((matched = reg.exec(num)) !== null){
arr.push(matched[0]);
reg.lastIndex = ++i;
}
arr = arr.sort(function(a, b){ return b - a;});
var leng = arr.length;
var result = [];
for(var j=0; j<leng; j++){
if(arr[0]>arr[j]){
break;
}
result.push(arr[j]);
}
if(result.length === 1){
return result[0];
}
return result;
}
}
写一个
function maxFiveDigits(num){
num = num + "";//转换为字符
var max = 0,//最大值
now = 0;//当前值
for(var i = 0,n=num.length-4;i<n;i++){
now = num.substring(i,i+5);//截取连续5位数
if(now > max)//如果当前值比最大值大 交换位置
max = now;
}
return max;
}
来个更新,对比了几个算法的性能(假设num为字符类型----省去转换的一步) ----------------------------------------分割线------------------------------------------------
//随机填充数据
//生成count位的随机数
function randomFill(count){
var ran = [];
for(var i = 0 ; i < count ; i++)
ran.push(Math.ceil(Math.random()*9));
return ran.join("");
}
var num = randomFill(100000);//10万位, (如果用100万位数的话,Math.max 会报错)
function maxFiveDigits(num){
var max = 0,//最大值
now = 0;//当前值
for(var i = 0,n=num.length-4;i<n;i++){
now = num.substr(i,5);//截取连续5位数
if(now > max)//如果当前值比最大值大 交换位置
max = now;
}
return max;
}
//减少substr 调用次数
function maxFiveDigits2(num){
var d = num[0],
max = num.substr(0,5),
now = 0;
for(var i = 1 , n = num.length - 4 ; i < n ; i ++ ){
if ( num[i] < d )continue;
d =num[i];
now = num.substr(i,5);
if( now > max )
max = now;
}
return max;
}
//利用Math.max 比较
function maxFiveDigits3(num) {
var str = num + '',arr = [];
if(str.length < 5){
return
}else{
for( var i = 0; i < str.length - 4; i ++){
arr.push(str.substr(i,5))
}
}
return Math.max.apply(window, arr);
}
------为了保证测试的准确性,随机调用了8次,最终 “减少substr调用次数” 的算法效率最高。------
var start = Date.now();
console.log(maxFiveDigits(num));
console.log(Date.now() - start);//8ms 8ms 9ms 9ms 4ms 10ms 5ms 9ms
start = Date.now();
console.log(maxFiveDigits2(num));
console.log(Date.now() - start);//7ms 6ms 6ms 6ms 5ms 6ms 5ms 5ms
start = Date.now();
console.log(maxFiveDigits3(num));
console.log(Date.now() - start);//15ms 15ms 16ms 10ms 17ms 12ms 16ms 16ms
function maxFiveDigits(num) {
var arr = new Array();
var str = num + "";
if(str.length < 5) {
return ;
}
var max = 0, temp1, temp2;
for(var i=0,len=str.length; i<len-4; i++) {
temp1 = str.substr(i, 5);
temp2 = temp1 - '0';
if(temp2 > max) {
max = temp2;
}
}
return max;
}
贴个有意思的
function maxFiveDigits(num) {
var d = num.toString();
var res = 0;
var tmp = 0;
for(var i = 0; i < d.length; i++) {
res = Math.max(res, tmp = (tmp % 10000) * 10 + Number(d[i]));
}
return res;
}
function maxFiveDigits(num) {
//TODO:完成该函数
if(!num){return false;}
num =num +'';
var sy ='';
for(var i = 0; i < num.length; i++){
if(i+5 > num.length){return sy;}
var slit = parseInt(num.substring(i ,i+5));
if(sy > slit){
sy = sy;
}else{
sy = slit;
}
}
}
parseInt为解决有的字符串为0开头
(() => {
function maxFiveDigits(num, size = 5) {
num = String(num)
return Math.max(...new Array(-size - ~num.length).fill().map((_, index) => parseInt(num.substr(index, size))))
}
console.log(maxFiveDigits(283910356876))
console.log(maxFiveDigits(123456))
})()