Java solution


  • 0
    J

    Just a simple java solution.

    public class Solution {
        public List<String> readBinaryWatch(int num) {
            int max = (1 << 10);
            int bound = (1 << 6);
            List<String> list = new ArrayList<String>();
            for(int i = 0 ; i <= max ; i++) {
                if(Integer.bitCount(i) == num) {
                    int hour = i / bound;
                    int min = i % bound;
                    if((hour < 12) && (min < 60))
                        list.add(intToTime(hour, min));
                }
            }
            return list;
        }
        String intToTime(int hour, int min){
            String result = hour + ":";
            if(min < 10)
                result += ("0" + min);
            else 
                result += min;
            return result;
        }
    }
    

Log in to reply
 

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