Open nunnly opened 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;
}
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])
}
@nunnly 提交的有问题,因为JavaScript中得数字类型是双精度浮点小数
,保留的位数非常有限。
parseInt(digits)会将较大的数字转为科学技术并丧失精度。例:
parseInt("239874982374892374982347") // 2.398749823748924e+23
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;
}
上个高效点的先:
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;
}
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;
}
@nunnly 应该传字符串,不应该传数字,高精度达不到那么高。
然后来个非主流版:
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];
}
@XadillaX 怎么才能达到您的境界?
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
}
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; }
var str = "12123343889948040009"; var reg = /\d(?=(\d{5}))/g; console.log(str.replace(reg,"$1,"));
@XadillaX 这个正则能否达到效果?怎么改?
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;
}
@Jonavin 我对正则不熟悉。
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);
}
function solution(s) {
var r = /(?=(\d{4}))/g,
rt = s.replace(r, "$1,").split(",");
return Math.max.apply(null, rt);
}
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 大神的好