nunnly / everycode

Javascript 每日一练
116 stars 26 forks source link

2014年12月4日 D5 #19

Open nunnly opened 9 years ago

nunnly commented 9 years ago
/* 
 * 根据传入的数字,返回其中包含的最大的连续五位数
 * e.g.
 * solution(283910356876)   // =>91035
 * solution(123456) //  => 23456
 * param Number String(可以转换为数字的字符串)
 * PS:传入的参数最大长度为1000位
 * return Number
 */

function solution(digits){

}
Bosn commented 9 years ago
function solution(digits){
    var str = digits + '';
    var max = 0;
    var i = 0;
    var n = str.length - 5;
    var sub = 0;
    for (; i <= n; i++) {
        sub = +str.substring(i, i + 5);
        if (sub > max) {
            max = sub;
        }
    }
    return max;
}
nunnly commented 9 years ago
function solution(digits){
  digits = parseInt(digits) + "";
  var greatest = digits.split("").sort()[digits.length-1];
  var reg = new RegExp(greatest + "\\d{4}");
  return parseInt(digits.match(reg).sort().reverse()[0])
}
Bosn commented 9 years ago

@nunnly 提交的有问题,因为JavaScript中得数字类型是双精度浮点小数,保留的位数非常有限。 parseInt(digits)会将较大的数字转为科学技术并丧失精度。例:

parseInt("239874982374892374982347") // 2.398749823748924e+23
bh-lay commented 9 years ago

史上最中规中矩的答案来啦

function solution (digits){
  if(!digits || parseInt(digits) != digits){
    return;
  }
  var maxNum = 0;
  for(var i=0,total=digits.length-5;i<total;i++){
     var thisNum = parseInt(digits.slice(i,i+5));
     console.log('thisNum:' + thisNum);
     if(thisNum > maxNum){
       maxNum = thisNum;
     }
  }
  return maxNum;
}
XadillaX commented 9 years ago

上个高效点的先:

function solution(digits) {
    var d = digits.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;
}
replace5 commented 9 years ago
function solution(str) {
    str = String(str).replace(/[^\d]/g, '');
    if (str.length < 5) {return null}   
    var lastMax = 0;
    for (var i = 0; i < str.length - 4; i++) {
        var cur = +(str.substr(i, 5));

        if (cur > lastMax) {
            lastMax = cur;
        }
    }

    return lastMax;
}
XadillaX commented 9 years ago

@nunnly 应该传字符串,不应该传数字,高精度达不到那么高。

XadillaX commented 9 years ago

然后来个非主流版:

function solution(a) {
    return a.toString().split("").reduce(function(a, b) {
        return [Math.max(a[0], 10 * (a[1] % 1e4) + (b | 0)), 10 * (a[1] % 1e4) + (b | 0)]
    }, [0, 0])[0];
}
Sanford2020 commented 9 years ago

@XadillaX 怎么才能达到您的境界?

singone commented 9 years ago

  function solution(digits){
        var strd,
                i= 0,
                len,
                maxnum =0,
                maxall = 0;

        if(typeof  digits === 'number'){
            strd=digits+''
        }else if(typeof digits ==='string'){
            strd=digits.trim();
        }else{
            return 0
        }

        len=strd.length - 5;
        if(len < 0){
            return digits
        }
        for(;i<len;i++){
            if(maxnum <= strd[i]){
                maxnum = strd[i];
                maxall= Math.max(maxall,parseInt((strd.slice(i,i+5))));
            }
        }
        return maxall
    }
lonphy commented 9 years ago

function solution(b){ b += ''; var i=10,m; while(i-- > 0) { m = b.match(new RegExp(i+"\d{4}", "g")); if (m){ return Math.max.apply(null,m); } } return 0; }

Sanford2020 commented 9 years ago

var str = "12123343889948040009"; var reg = /\d(?=(\d{5}))/g; console.log(str.replace(reg,"$1,"));

@XadillaX 这个正则能否达到效果?怎么改?

xlitter commented 9 years ago
function solution(digits) {
    var source, 
          maxD,
          index,
          maxNum = -1;
    if (!(/^\d+$/.test(digits))) {
        console.log('! number');
        return;
    }

    source = digits.split('');
    wSource = source.slice(0, source.length - 4);
    maxD = Math.max.apply(null, wSource);
    while ((index = wSource.indexOf(maxD + '', index + 1)) != -1) {
        maxNum = Math.max(parseInt(source.slice(index, index + 5).join('')), maxNum);
    }
    return maxNum;
}
XadillaX commented 9 years ago

@Jonavin 我对正则不熟悉。

CraigZeng commented 9 years ago
function solution(digits){
   var result = [];
   (digits+'').replace(/\d(?=\d{4})/g, function(match, pos, str){
      result.push(+str.substr(pos, 5));
   });
   return Math.max.apply(null, result);
}
Sanford2020 commented 9 years ago
function solution(s) {
    var r = /(?=(\d{4}))/g,
    rt = s.replace(r, "$1,").split(",");
    return Math.max.apply(null, rt);
}
VaJoy commented 9 years ago
  function solution(s) {
    var r = /(?=(\d{4}))/g,
        rt = s.replace(r, "$1,").split(",");
    return Math.max.apply(null, rt);
  }
  console.log(solution("345455395866"));

不过这个要遍历2次,没XadillaX 大神的好