C++ Binary Watch


  • 1
    X

    class Solution {
    public:
    vector<string> readBinaryWatch(int num) {
    int i,j;
    vector<string> vec;

        for(i=0;i<12;i++){
            for(j=0;j<60;j++){
                if(bit(i)+bit(j) ==num){
                    vec.push_back(to_string((long long)i)+(j<10?":0":":")+to_string((long long)j));
                }
            }
        }
        return vec;
    }
    //how many 1 in m
    int bit(int m){
        int i=0;
        if(m==1) return 1;
        if(m<1) return 0;
        while(m){
    		m&=m-1;
            i++;
        }
        return i;
    }
    

    };


Log in to reply
 

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