Straightforward and self explaining java solution


  • 1
    D
    public List<String> readBinaryWatch(int num) {
        List<String> times = new ArrayList<String>();
        int hour, minute;
        for(hour=0; (minute=(num - hour))>=0 && hour<=3; hour++){
            for(int h=0; h<12; h++){
                if(Integer.bitCount(h) == hour){
                    for(int m=0; m<60; m++){
                        if(Integer.bitCount(m) == minute){
                            String min = Integer.toString(m);
                            if(min.length()==1) min = "0"+min;
                            String s = Integer.toString(h)+":"+min;
                            times.add(s);
                        }
                    }
                }
            }
        }
        return times;
    }

Log in to reply
 

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