My Javascript solution, 140ms


  • -1
    M

    var countDigitOne = function(n) {
    var sum = 0;
    var x = 1;
    while(x<=n) {
    var upNum = Math.floor(n/(10x));
    var lowNum = n % (10
    x);
    var vs = Math.floor(lowNum/x) - 1;
    if(vs>0) {
    sum += (upNum + 1) * x;
    } else if (vs===0) {
    sum += upNumx + lowNum%x + 1;
    } else if (vs<0) { //vs==-1
    sum += upNum
    x;
    }
    x = x * 10;
    }
    return sum;
    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.