0ms Back-Tracking C++ Solution, Clean and Easy-understanding


  • 0
    S
    class Solution {
    public:
        void helper(int num, int index, vector<string> &res, int tar){
            if(num==0){
                int hour = tar>> 6;
                int min = tar-(hour<<6);
                if(hour>11 || min>59){
                    return;
                }
                res.push_back(to_string(hour)+(min<10?":0":":")+to_string(min));
                return;
            }
            for(int i=index;i<10;++i){
                helper(num-1,i+1,res,tar|(1<<i));
            }
        }
    
        vector<string> readBinaryWatch(int num) {
            vector<string> res;
            helper(num,0,res,0);
            return res;
        }
    };
    

Log in to reply
 

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