less than 1 minute read

<-E 401> Binary Watch

class Solution {
public:
    int numbits(int x) {
        int cnt = 0;
        while(x) {
            x &= x - 1;
            cnt++;
        }
        return cnt;
    }
    
    vector<string> readBinaryWatch(int num) {
        
        vector<string> res;
        const int upper_limit = (1<<10);
        
        for(int i = 0;i< upper_limit;i++) {
            if(numbits(i) != num) continue;
            int hrs = i >> 6;
            int mn = i % (1 << 6);
            if(hrs < 12 && mn < 60) {
                string time = to_string(hrs) + ":" + 
                    (mn < 10 ? "0" : "") + to_string(mn);
                res.push_back(time);
            }
        }
        return res;
        
    }
};