JavaScript Solution


  • 0
    R
    /**
     * @param {number} num
     * @return {string[]}
     */
    var readBinaryWatch = function(num) {
        var result = [];
        for (var i = 0; i <= 11; i++) {
            for (var j = 0; j <= 59; j++) {
                if ((countSetBit(i) + countSetBit(j)) === num) {
                    result.push(i + (j < 10 ? ':0' : ':') + j);
                }
            }
        }
        return result;
    };
    
    function countSetBit(num) {
        var binary = num.toString(2);
        var count = 0;
        for (var i = 0; i < binary.length; i++) {
            if (binary[i] == 1) {
                count++;
            }
        }
        return count;
    }
    

Log in to reply
 

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