3 ms Java solution


  • 0
    public class Solution {
        public List<String> readBinaryWatch(int num) {
            List<Integer> result = new ArrayList<Integer>();
            int[] vals = {800,400,200,100,32,16,8,4,2,1};
            search(result, num, 0, 0, vals);
            
            List<String> ans = new ArrayList<String>();
            for(Integer value:result)
            {
                ans.add(value/100 + ":" + (value%100 < 10 ? "0" : "") + value%100);
            }
            return ans;
        }
        
        public void search(List<Integer> result, int num, int pos, int total, int[] vals)
        {
            if(num == 0)
            {
                if(total/100 < 12 && total%100 < 60)
                    result.add(total);
                return;
            }
            
            if(pos == vals.length) return;
            //Light pos
            search(result, num -1, pos + 1, total + vals[pos], vals);
            //Do not light pos
            search(result, num, pos + 1, total, vals);
        }
    }
    

Log in to reply
 

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