Leetcode - Binary Watch

A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).git

Each LED represents a zero or one, with the least significant bit on the right.spa

For example, the above binary watch reads "3:25".code

Given a non-negative integer n which represents the number of LEDs that are currently on, return all possible times the watch could represent.orm

Example:blog

Input: n = 1
Return: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"]
  • The order of output does not matter.
  • The hour must not contain a leading zero, for example "01:00" is not valid, it should be "1:00".
  • The minute must be consist of two digits and may contain a leading zero, for example "10:2" is not valid, it should be "10:02".

=======================SOLUTION=============================get

這道題個人第一個想法就是 這個好像和 letter combintion of phone number 那道題目很像,咱們能夠算出當lights number爲k時全部可能的hours再算出相對應 num-k時全部可能minutes, 而後進行組合。 這樣的話,我就想着咱們應該分步進行。string

  • 第一步, 算出hours
  • 第二步, 算出minutes
  • 第三步, 結合hours 和 minutes
  • 第四步, 改變 hours lights的個數, 繼續上述

固然 咱們須要注意的是亮燈個數, 由於hours畢竟一共才4燈,minutes一共才6燈,因此要根據傳入的燈總數進行調整。同時, 咱們也要注意時針不能夠超過11,分針不能夠超過59.it

代碼:io

 

class Solution {
    int HOUR_LIMIT = 11;
    int MINUTE_LIMIT = 59;
    public List<String> readBinaryWatch(int num) {
        int[] hours = new int[]{1,2,4,8};
        int[] minutes = new int[]{1,2,4,8,16,32};
        List<String> hoursOptions = new ArrayList<>();
        List<String> minutesOptions = new ArrayList<>(); 
        List<String> results = new ArrayList<>();
        //separate the formation of hour and minute
        int options = num > hours.length ? hours.length : num;
        for (int index = 0; index <= options; index++) {
            formLightComb(index, hoursOptions, hours, 0, 0, HOUR_LIMIT);
            int option = minutes.length >= num - index ? num - index : minutes.length;
            formLightComb(option, minutesOptions, minutes, 0, 0, MINUTE_LIMIT);
            formTime(minutesOptions, hoursOptions, results, 0, "");
            hoursOptions.clear();
            minutesOptions.clear();
        }
        return results;
    }
    
    void formLightComb(int num, List<String> results, int[] times, int time, int index, int limit) {
        if (num == 0) {
            if (time <= limit) {
                if (limit == MINUTE_LIMIT && (time / 10 == 0)) {
                    results.add("0" + time);
                } else {
                    results.add(time + "");
                }
            }
            return;
        }
        for (int i = index; i < times.length; i++) { 
            formLightComb(num - 1, results, times, times[i] + time, i + 1, limit);
        }
    }
    
    void formTime(List<String> minutes, List<String> ls, List<String> results, int count, String time){
        if (count == 2) {
            results.add(time.substring(1, time.length()));
            return;
        }
        for (int i = 0; i < ls.size(); i++) {
             formTime(minutes, minutes, results, count + 1, time + ":" + ls.get(i));
        }           
    }
}
相關文章
相關標籤/搜索