[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).html

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

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

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

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:ide

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

 

這道題考察咱們二進制表,說實話,博主對二進制表無感,感受除了裝b沒啥其餘的做用,誰會看個時間還要算半天啊,可是這並不影響咱們作題,咱們首先來看一種寫法很簡潔的解法,這種解法利用到了bitset這個類,能夠將任意進制數轉爲二進制,並且又用到了count函數,用來統計1的個數。那麼時針從0遍歷到11,分針從0遍歷到59,而後咱們把時針的數組左移6位加上分針的數值,而後統計1的個數,即爲亮燈的個數,咱們遍歷全部的狀況,當其等於num的時候,存入結果res中,參見代碼以下: 函數

 

解法一:
class Solution {
public:
    vector<string> readBinaryWatch(int num) {
        vector<string> res;
        for (int h = 0; h < 12; ++h) {
            for (int m = 0; m < 60; ++m) {
                if (bitset<10>((h << 6) + m).count() == num) {
                    res.push_back(to_string(h) + (m < 10 ? ":0" : ":") + to_string(m));
                }
            }
        }
        return res;
    }
};

 

上面的方法之因此那麼簡潔是由於用了bitset這個類,若是咱們不用這個類,那麼應該怎麼作呢?這個燈亮問題的本質其實就是在n個數字中取出k個,那麼就跟以前的那道Combinations同樣,咱們能夠借鑑那道題的解法,那麼思路是,若是總共要取num個,咱們在小時集合裏取i個,算出和,而後在分鐘集合裏去num-i個求和,若是兩個都符合題意,那麼加入結果中便可,參見代碼以下:post

 

解法二:url

class Solution {
public:
    vector<string> readBinaryWatch(int num) {
        vector<string> res;
        vector<int> hour{8, 4, 2, 1}, minute{32, 16, 8, 4, 2, 1};
        for (int i = 0; i <= num; ++i) {
            vector<int> hours = generate(hour, i);
            vector<int> minutes = generate(minute, num - i);
            for (int h : hours) {
                if (h > 11) continue;
                for (int m : minutes) {
                    if (m > 59) continue;
                    res.push_back(to_string(h) + (m < 10 ? ":0" : ":") + to_string(m));
                }
            }
        }
        return res;
    }
    vector<int> generate(vector<int>& nums, int cnt) {
        vector<int> res;
        helper(nums, cnt, 0, 0, res);
        return res;
    }
    void helper(vector<int>& nums, int cnt, int pos, int out, vector<int>& res) {
        if (cnt == 0) {
            res.push_back(out);
            return;
        }
        for (int i = pos; i < nums.size(); ++i) {
            helper(nums, cnt - 1, i + 1, out + nums[i], res);
        }
    }
};

 

下面這種方法就比較搞笑了,是博主在無法想出上面兩種方法的狀況下萬般無奈使用的,你個二進制表再叼也就72種狀況,全給你列出來,而後採用跟上面那種解法相同的思路,時針集合取k個,分針集合取num-k個,而後存入結果中便可,參見代碼以下:idea

 

解法三:

class Solution {
public:
    vector<string> readBinaryWatch(int num) {
        vector<vector<int>> hours{{0},{1,2,4,8},{3,5,9,6,10},{7,11}};
        vector<vector<int>> minutes{{0},{1,2,4,8,16,32},{3,5,9,17,33,6,10,18,34,12,20,36,24,40,48},{7,11,19,35,13,21,37,25,41,49,14,22,38,26,42,50,28,44,52,56},{15,23,39,27,43,51,29,45,53,57,30,46,54,58},{31,47,55,59}};
        vector<string> res;
        for (int k = 0; k <= num; ++k) {
            int t = num - k;
            if (k > 3 || t > 5) continue;
            for (int i = 0; i < hours[k].size(); ++i) {
                for (int j = 0; j < minutes[t].size(); ++j) {
                    string str = minutes[t][j] < 10 ? "0" + to_string(minutes[t][j]) : to_string(minutes[t][j]);
                    res.push_back(to_string(hours[k][i]) + ":" + str);
                }
            }
        }
        return res;
    }
};

 

參考資料:

https://discuss.leetcode.com/topic/59374/simple-python-java

https://discuss.leetcode.com/topic/59401/straight-forward-6-line-c-solution-no-need-to-explain

https://discuss.leetcode.com/topic/59494/3ms-java-solution-using-backtracking-and-idea-of-permutation-and-combination/2

 

LeetCode All in One 題目講解彙總(持續更新中...)

相關文章
相關標籤/搜索