Expand decimal number so each digit is displayed as its actual numerical value, i.e "87.1" -> [80, 7, 0.1]


  • 0
    D

    For example, for input n = "7970521.5544" the output will be ["7000000", "900000", "70000", "500", "20", "1", ".5", ".05", ".004", ".0004"]. My solution (JavaScript):

    function expand(n){
        var result = [];
        var ref = n.indexOf('.') !== -1 ? n.indexOf('.') : n.length;
        for(var i = 0; i < n.length; i++){
            if(n.charAt(i) === '0' || i === ref) continue;
            var zeros = '';
            var j = 0;
            if(i < ref){
                while(j < ref-i-1){
                    zeros = zeros.concat('0');
                    j++;
                }
                result.push(n.charAt(i)+''+zeros);
            }else{
                while(j < (i-ref-1)){
                    zeros = zeros.concat('0');
                    j++;
                }
                result.push('.'+zeros+''+n.charAt(i));
            }
        }
        return result;
    }

Log in to reply
 

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