0ms simple brute-force C++ solution


  • 0
    N
    class Solution {
    private:
        int cntNum(int n){
            int ret = 0;
            while (n){
                n &= n - 1;
                ret ++;
            }
            return ret;
        }
        
    public:
        vector<string> readBinaryWatch(int num) {
            vector<string> ret;
            for (int i = 0; i < 12; ++ i)
                for (int j = 0; j < 60; ++ j){
                    if (cntNum(i) + cntNum(j) == num) 
                        ret.push_back(to_string(i) + ":" + (j < 10 ? "0" : "") + to_string(j));
                }
            return ret;
        }
    };
    

Log in to reply
 

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