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

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

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

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

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:

  • 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個求和,如果兩個都符合題意,那么加入結果中即可,參見代碼如下:

 

解法二:

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個,然后存入結果中即可,參見代碼如下:

 

解法三:

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 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM