JavaScript solution


  • 0
    var readBinaryWatch = function(num) {
        var leds = [8, 4, 2, 1, 32, 16, 8, 4, 2, 1];
        var result = [], indices = [];
        var hours = 0;
        var minutes = 0;
        
        function iterTool(arr, i) {
            if(arr.length == num) {
                indices.push(arr);
                return;
            }
            if(i + 1 > leds.length) return;
            
            iterTool(arr.concat(i), i + 1);
            iterTool(arr, i + 1);
        }
        
        iterTool([], 0);
        
        for(let indice of indices) {
            hours = minutes = 0;
            for(let val of indice) {
                if(val < 4) hours += leds[val];
                else minutes += leds[val];
            }
            if(hours > 11 || minutes > 59) continue;
            // result.push(hours + ":" + minutes.toString().padStart(2, 0)); // ES8
            minutes = minutes < 10 ? ("0" + minutes) : minutes;
            result.push(hours + ":" + minutes);
    
        }
        
        return result;
    };
    

Log in to reply
 

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