3ms concise Java Solution


  • 0
    C
    public class Solution {
        public List<String> readBinaryWatch(int num) {
            List<String> res=new ArrayList<>();
            if(num>=9) return res;
            int[] map=new int[num];
            dfs(res, -1, map, num);
            return res;
        }
        private void dfs(List<String> res, int index, int[] map, int num){
            if(num==0){
                int i=0,h=0,m=0;
                while(i<map.length){
                    if(map[i]<=3) h+=Math.pow(2,map[i++]);
                    else m+=Math.pow(2,map[i++]-4);
                }
                if(h>11||m>59) return;
                if(m<10) res.add(h+":0"+m);
                else res.add(h+":"+m);
                return;
            }
            if(index==9) return;
            int t=map.length-num;
            for(int i=index+1;i<10;i++){
                map[t]=i;
                dfs(res, i,map,num-1);
            }
        }
    }
    

Log in to reply
 

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