leetcode--401. Binary Watch

一、問題描述html

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).java

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

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

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

Example:函數

Input: n = 1
Return: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"]

Note:spa

  • 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".

二、邊界條件:一、num比較大的狀況,在前面就能夠處理掉,後面就能夠不用考慮了;二、num = 0的狀況;三、num<0不考慮,其餘也在hour的循環裏面過濾掉了。code

三、思路:一、先肯定hour的亮燈數,for循環;二、num - hour即爲min的亮燈數,校驗一下合法性;三、用遞歸的方法,分別求出hour和min的亮燈組合;四、最後將hour和min組合起來。orm

遞歸子函數思路:一、來到一個位置,有兩種選擇-亮燈或者不亮燈;二、選擇不亮燈,亮燈數不變,index+1,可是有限制條件--剩餘位置>剩餘亮燈數,不然必須選擇亮燈;三、選擇亮燈,亮燈數-1,index+1;四、進入下一層。htm

base case:亮燈數==0,一、把int[] 記錄的亮燈分佈,計算成時/分;二、hour不用多處理,直接轉爲String保存,min須要判斷>9?決定是否添0。

四、代碼實現

class Solution {
    public List<String> readBinaryWatch(int num) {
        List<String> results = new ArrayList<>();
        for (int hourLedOnNum = 0; hourLedOnNum < 4 && hourLedOnNum <= num; hourLedOnNum++) {
            int minLedOnNum = num - hourLedOnNum;
            if (minLedOnNum >= 6) {
                continue;
            }
            List<String> hours = new ArrayList<>();
            int[] hourLeds = {0, 0, 0, 0};
            timeCombs(hours, hourLeds, hourLedOnNum, 0);

            List<String> mins = new ArrayList<>();
            int[] minLeds = {0, 0, 0, 0, 0, 0};
            timeCombs(mins, minLeds, minLedOnNum, 0);

            results.addAll(watchCombs(hours, mins));
        }
        return results;
    }

    public void timeCombs(List<String> times, int[] leds, int ledOnNum, int index) {
        if (ledOnNum == 0 || index == leds.length) {//後面條件能夠去掉
            //record reuslts
            int time = 0;
            for (int i = 0; i < leds.length; i++) {
                time += leds[i] << i;
            }
            if ((leds.length == 4 && time > 11)
                || (leds.length == 6 && time > 59)) {
                return;
            }
            if (leds.length == 6 && time < 10) {//min小於10須要加0
                times.add("0" + Integer.toString(time));
            } else {
                times.add(Integer.toString(time));//Integer.toString(int i) int i 轉成 String
            }
            return;
        }
        if (ledOnNum < leds.length - index) {
            timeCombs(times, leds, ledOnNum, index + 1); //not choose
        }
        leds[index] = 1; //choose
        timeCombs(times, leds, ledOnNum - 1, index + 1);
        leds[index] = 0;
    }

    public List<String> watchCombs(List<String> hours, List<String> mins) {
        List<String> results = new ArrayList<>();
        for (int i = 0; i < hours.size(); i++) {
            for (int j = 0; j < mins.size(); j++) {
                results.add(hours.get(i) + ":" + mins.get(j));
            }
        }
        return results;
    }
}

 五、時間複雜度:; 空間複雜度:

六、主要api:

int類型轉成String:String Integer.toString(int i),i是被轉換,返回值爲String
Integer類型轉爲String: Integer i = new Integer(); String str = i.toString();
相似的還有 Character.toString(char c); Character ch = new Character(); ch.toString();
七、其餘解法:
從結果上面篩選,數一下bit數
    public List<String> readBinaryWatch(int num) {
        ArrayList<String> result = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            for (int j = 0; j < 60; j++) {
                if (Integer.bitCount(i) + Integer.bitCount(j) == num) {
                    result.add(String.format("%d:%02d", i, j));
                }
            }
        }
        return result;
    }
相關文章
相關標籤/搜索