Easy Java 4ms Solution


  • 0
    2
    public List<String> readBinaryWatch(int num)
    	{
    		int[] hourArray={1,2,4,8};//1,2,4,8
    		int[] minArray={1,2,4,8,16,32};//1,2,4,8,16,32
    		List<Integer> hours=new ArrayList<>();
    		List<Integer> mins=new ArrayList<>();
    		List<String> res=new ArrayList<>();
    		for(int i=0;i<=num;i++)
    		{
    			selectHour(i,0,hours,hourArray,0);
    			selectMin(num-i,0,mins,minArray,0);
    			for(Integer hour:hours)
    			{
    				for(Integer min:mins)
    				{
    					res.add(hour+":"+ (min<10?"0"+min:min));
    				}
    			}
    			hours.clear();
    			mins.clear();
    		}
    		return  res;
    	}
    	
    	public void selectHour(int num,int curVal,List<Integer> hours,int[] hourArray,int start)
    	{
    		if(num==0&&curVal<12)
    		{
    			hours.add(curVal);
    			return;
    		}
    		for(int i=start;i< hourArray.length;i++  )
    		{
    			int val=curVal+hourArray[i];
    		
    			selectHour(num-1, val, hours, hourArray,i+1);
    		}
    	}
    	
    	public  void  selectMin(int num,int curVal,List<Integer> mins,int[] minArray,int start)
    	{
    		if(num==0&&curVal<60)
    		{
    			mins.add(curVal);
    			return;
    		}
    		
    		for(int i=start;i< minArray.length;i++)
    		{
    			int val=curVal+minArray[i];
    			selectMin(num-1, val, mins, minArray,i+1);
    		}
    	}
    

  • 0
    O

    A binary watch has 4 LEDs on the top which represent the hours (0-11)
    Hours is between 0 and 11.
    I made the same mistake


  • 0
    2

    @OwenYY yes, you are right


  • 0
    S

    if num=2,how about 12:00?


  • 0
    2

    @sxy0707 sorry,i don't get your point。 if num=2,it doesn't contain 12:00, 12:00 is the same as 0:00 when num=1.


  • 0
    2

    @278111013qq.com sorry ,there is a mistake, it is 0:00 when num=0;


Log in to reply
 

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