Easy Java Answer


  • 0
    L

    Easy Java Answer

    public class Solution {
        public List<String> readBinaryWatch(int num) {
            int[] list = new int[10];
            List<String> res = new ArrayList<String>();
            dfs(res, list, 0, 0, num);
            return res;
        }
        
        private void dfs(List<String> res, int[] list, int count, int index, int num) {
            if (count == num) {
                int hour = 0, min = 0, i = 0;
                for (; i < 4; i++)
                    hour = (hour << 1) + list[i];
                for (; i < 10; i++)
                    min = (min << 1) + list[i];
                if (hour < 12 && min < 60)
                    res.add(hour + ":" + (min < 10 ? "0" : "") + min);
                return;
            }
            for (int i = index; i < 10; i++) {
                list[i] = 1;
                dfs(res, list, count + 1, i + 1, num);
                list[i] = 0;
            }
        }
    }

Log in to reply
 

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