C# solution based on one of the C++ solutions


  • 0
    M
    public IList<string> ReadBinaryWatch(int num) {
        IList<string> result = new List<string>(); 
        
        for (int i = 0; i < 12; i++)
        {
            for (int j = 0; j < 60; j++)
            {
                if (GetBitCount(i) + GetBitCount(j) == num)
                {
                    result.Add(string.Format("{0}:{1}",i, j.ToString().PadLeft(2,'0')));
                }
            }
        }
        
        return result; 
    }
    
    int GetBitCount(int num)
    {
        int sum = 0; 
        while (num > 0)
        {
            sum += num & 1; 
            num >>= 1; 
        }
        
        return sum; 
    }

Log in to reply
 

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