|
| 1 | +// Source : https://leetcode.com/problems/binary-watch/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2016-11-05 |
| 4 | + |
| 5 | +/*************************************************************************************** |
| 6 | + * |
| 7 | + * A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 |
| 8 | + * LEDs on the bottom represent the minutes (0-59). |
| 9 | + * Each LED represents a zero or one, with the least significant bit on the right. |
| 10 | + * |
| 11 | + * For example, the above binary watch reads "3:25". |
| 12 | + * |
| 13 | + * Given a non-negative integer n which represents the number of LEDs that are |
| 14 | + * currently on, return all possible times the watch could represent. |
| 15 | + * |
| 16 | + * Example: |
| 17 | + * Input: n = 1Return: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", |
| 18 | + * "0:16", "0:32"] |
| 19 | + * |
| 20 | + * Note: |
| 21 | + * |
| 22 | + * The order of output does not matter. |
| 23 | + * The hour must not contain a leading zero, for example "01:00" is not valid, it |
| 24 | + * should be "1:00". |
| 25 | + * The minute must be consist of two digits and may contain a leading zero, for example |
| 26 | + * "10:2" is not valid, it should be "10:02". |
| 27 | + ***************************************************************************************/ |
| 28 | + |
| 29 | +class Solution { |
| 30 | +private: |
| 31 | + void combination(int len, int n, int max, bool zero, |
| 32 | + int start, int k, int solution, |
| 33 | + vector<vector<string>>& result) { |
| 34 | + if (solution > max){ |
| 35 | + return; |
| 36 | + } |
| 37 | + if (k == 0) { |
| 38 | + char tmp[5] = ""; |
| 39 | + if (zero) { |
| 40 | + sprintf(tmp, "%02d", solution); |
| 41 | + }else{ |
| 42 | + sprintf(tmp, "%d", solution); |
| 43 | + } |
| 44 | + result[n].push_back(tmp); |
| 45 | + return; |
| 46 | + } |
| 47 | + for (int i=start; i<=len-k; i++) { |
| 48 | + solution += pow(2, i); |
| 49 | + combination(len, n, max, zero, i+1, k-1, solution, result); |
| 50 | + solution -= pow(2, i); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + void generate_combination(int nLED, int max, bool zero, vector<vector<string>>& result) { |
| 55 | + for (int i=0; i<nLED; i++) { |
| 56 | + combination(nLED, i, max, zero, 0, i, 0, result); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + void print(vector<vector<string>>& vv) { |
| 61 | + for(auto v : vv) { |
| 62 | + cout << "[ "; |
| 63 | + for (auto i : v) { |
| 64 | + cout << i << " "; |
| 65 | + } |
| 66 | + cout << "]" << endl; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | +private: |
| 71 | + vector<vector<string>> hour; |
| 72 | + vector<vector<string>> mins; |
| 73 | + |
| 74 | +public: |
| 75 | + |
| 76 | + Solution():hour(4, vector<string>()), mins(6, vector<string>()){ |
| 77 | + generate_combination(4, 11, false, hour); |
| 78 | + //print(hour); |
| 79 | + //[ 0 ] |
| 80 | + //[ 1 2 4 8 ] |
| 81 | + //[ 3 5 9 6 10 ] |
| 82 | + //[ 7 11 ] |
| 83 | + |
| 84 | + |
| 85 | + generate_combination(6, 59, true, mins); |
| 86 | + //print(mins); |
| 87 | + //[ 00 ] |
| 88 | + //[ 01 02 04 08 16 32 ] |
| 89 | + //[ 03 05 09 17 33 06 10 18 34 12 20 36 24 40 48 ] |
| 90 | + //[ 07 11 19 35 13 21 37 25 41 49 14 22 38 26 42 50 28 44 52 56 ] |
| 91 | + //[ 15 23 39 27 43 51 29 45 53 57 30 46 54 58 ] |
| 92 | + //[ 31 47 55 59 ] |
| 93 | + } |
| 94 | + |
| 95 | + vector<string> readBinaryWatch(int num) { |
| 96 | + |
| 97 | + vector<string> result; |
| 98 | + for (int i = 0; i <= 3 && i <= num; i++) { |
| 99 | + if (num - i > 5) { |
| 100 | + continue; |
| 101 | + } |
| 102 | + for (auto h : hour[i]) { |
| 103 | + for (auto m : mins[num - i]) { |
| 104 | + result.push_back( h + ":" + m ); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | + return result; |
| 110 | + } |
| 111 | +}; |
0 commit comments