java with backtracking


  • 0
    3
    public class Solution {
        
        private List<String> result = new ArrayList<String>(); 
    	private int hour=0,minute=0;
    	private int[] LEDs = {8,4,2,1,32,16,8,4,2,1}; 
    	private int ledLen = 10;
    	public List<String> readBinaryWatch(int num) {
    		if(num == 0){
    			result.add("0:00");
    		}else{
    			for (int i = 0; i < ledLen; i++) {
    				_readBinaryWatch(i, num);
    			}
    		}
    		return result;
        }
    	
    	private void _readBinaryWatch(int i, int num){
    		if(ledLen-i < num) return;
    		
    		if(i<4){
    			if(hour + LEDs[i]>11) return;
    			hour = hour + LEDs[i];
    		}else{
    			if(minute + LEDs[i]>59) return;
    			minute = minute + LEDs[i];
    		}
    		
    		if(num==1) {
    			String s="";
    		    if(minute<10) s="0";
    		    result.add(hour+":"+s+minute);
    		}else{
    			for (int j = i+1; j < ledLen; j++) {
    				_readBinaryWatch(j, num-1);
    			}
    		}
    		
    		if(i<4){
    			hour = hour - LEDs[i];
    		}else{
    			minute = minute - LEDs[i];
    		}
    	}
    }
    

Log in to reply
 

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