Concise and Fast Java Solution


  • 0
    public class Solution {
        private List<String> res = new ArrayList<>();
        final int[] arr = {1,2,4,8,1,2,4,8,16,32};
        public List<String> readBinaryWatch(int num) {
            helper(num, 0, 0, 0);
            return res;
        }
        private void helper(int n, int hr, int min, int index) {
            if (n < 0 || hr >= 12 || min >= 60) return;
            if (n == 0) {
                String time = hr + ":" + (min > 9 ? min : "0" + min);
                res.add(time);
                return;
            }
            for (int i = index; i < arr.length; i++) {
                if (i < 4) helper(n - 1, hr + arr[i], min, i + 1);
                else helper(n - 1, hr, min + arr[i], i + 1);
            }
        }
    }
    

Log in to reply
 

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