Java solution


  • 0
    R
    public class Solution {
        public List<String> readBinaryWatch(int num) {
            ArrayList<String> times = new ArrayList<String>();
            
            for (int hour = 0; hour < 12; hour++) {
                if(getCount(hour) <= num) {
                    for (int min = 0; min < 60; min++) {
                        if(getCount(hour) + getCount(min) == num) {
                            times.add(hour+":"+ (min < 10 ? "0"+min : min));
                        }
                    }
                }
            }
            return times;
        }
    
        private int getCount(int n) {
            int count = 0;
            while(n > 0) {
                count += n & 1;
                n >>= 1;
            }
            return count;
        }
    }
    

  • 0
    I

    @rgilbertpaul Could you please provide the time complexity for this solution? Is it
    O(hour * min * (log(hour) + log(min)))


Log in to reply
 

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