Easy understand Java


  • 0
    C
    public class Solution {
         static int[] min = new int[]{1,2,4,8,16,32};
         static int[] hour = new int[]{1,2,4,8};
    	public static List<String> readBinaryWatch(int num) {
           
              List<String> res = new ArrayList<>();
    	        for(int i = 0; i <= Math.min(num, 4); i++){
    	            List<String> hours = new ArrayList<>(); 
                   List<String> mins = new ArrayList<>();
    	            getHours(i, hour, 0, 0, hours, 0);
    	            getMin(num-i,min,0,0,mins,0);
    	            for(String hour : hours){
    	                for(String min: mins){
    	                    if(!res.contains(hour+ ":"+min))
    	                    res.add(hour+ ":"+min);   
    	                }
    	            }
    	        }
    	        return res;
    	    }
    	    private static void getHours(int num, int[] hour, int index, int k, List<String>list, int sum){
    	        if(num == 0){
    	        	list.add("0");
    	            return ;
    	        }else{
    	           if(k == num && sum < 12){
    	               list.add(Integer.toString(sum));
    	               return;
    	           }
    	           for(int i = index; i < hour.length; i++){
       	            sum += hour[i];
       	         getHours(num,hour,i+1,k+1,list,sum);
       	            sum -= hour[i];
    	            }   
    	        }
    	    }
    	   private static void getMin(int num, int[] min, int index, int k , List<String> list, int sum){
    	        if(num == 0){
    	            list.add("00");
    	            return ;
    	        }
    	        if(k == num && sum <= 59){
    	            
    	            if(sum < 10) list.add("0" + Integer.toString(sum));
    	            else list.add(Integer.toString(sum));
    	            return;
    	        }
    	        if(index == min.length) return;
    	        
    	        for(int i = index; i < min.length; i++){
    	            sum += min[i];
    	            getMin(num,min,i+1,k+1,list,sum);
    	            sum -= min[i];
    	        }
    	   }
    }

Log in to reply
 

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