Short, easy-to-understand recursive solution.


  • 0
    U
    private void permutation(int number, int count, int start, List<String> result) {
        if (count == 0)
            if ((number & 15) < 12 && (number >> 4) < 60)
                result.add(String.format("%d:%02d", number & 15, number >> 4));
        for (int i = start; i < 10; i++)
            permutation(number | (1 << i), count - 1, i + 1, result);
    }
    
    public List<String> readBinaryWatch(int num) {
        List<String> result = new ArrayList();
        permutation(0, num, 0, result);
        return result;
    }

Log in to reply
 

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