only 0ms C++ solution easy understand


  • 0
    Y
    class Solution {
    public:
        int bitCount(int c)
        {
            int count=0;
            while (c)
            {
                c=c&(c-1);
                count++;
            }
            return count;
        }
        
        vector<string> readBinaryWatch(int num) {
            int bottom;
            vector<string> result;
                for (int i=0;i<=11;++i)
                {
                    bottom=num-bitCount(i);
                    if (bottom<0)   continue;
                    for (int j=0;j<60;++j)
                    {
                        if (bitCount(j)==bottom)
                        {
                            string s=to_string(i);
                            s=s+':';
                            if (j<10)   
                            {
                                s=s+'0';
                                s=s+to_string(j);
                            }
                            else    s+=to_string(j);
                            result.push_back(s);
                        }
                    }
                }
            return result;
        }
    };
    

  • 0
    Q

    Good job! Easy to understand!


Log in to reply
 

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