Java. Pure backtracking


  • 0
    S
    public class Solution {
        int[] binnary = {1,2,4,8,16,32};
        
        public List<String> readBinaryWatch(int num) {
            Set<String> result = new HashSet<>();
            iterate(0,0,num,0,0,result);
            return new ArrayList<String>(result);
        }
        
        private void iterate(int h, int m, int n, int sth, int stm, Set<String> res){
            if(n==0 && h<12 && m<60){
                res.add(String.format("%d:%02d",h,m));
                return;
            }
            
            for(int i = stm; i<6; i++){
                iterate(h,m+binnary[i],n-1,sth,i+1,res);
            }
            for(int i = sth; i<4; i++){
                iterate(h+binnary[i],m,n-1,i+1,stm,res);
            }
        }
    }
    

Log in to reply
 

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