C++ Solution Generating All Combinations Recursively, 3 ms


  • 0
    M
    class Solution {
    public:
        void GenRec(vector<string> & results, int num, int comb, int depth, int begin, int end) {
            if (depth >= num) {
                if ((comb >> 6) <= 11 && (comb & 63) <= 59)
                    results.push_back(to_string(comb >> 6) + ":" + ((comb & 63) < 10 ? "0" : "") + to_string(comb & 63));
            } else
                for (int i = begin; i < end; ++i)
                    GenRec(results, num, comb | (1 << i), depth + 1, i + 1, end + 1);
        }
        vector<string> readBinaryWatch(int num) {
            vector<string> result;
            GenRec(result, num, 0, 0, 0, 11 - num);
            return result;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.