0ms C++ Solution, Combination + Bitwise


  • 0
    P

    class Solution {
    public:

    string convertIntToTime(unsigned int time)
    {
        unsigned int h = time >> 28;
        unsigned int m = time >> 22;
    	m = m & (1 << 6) - 1;
    
    	if(h > 11 || m > 59)
    		return "";
    		
    	char buf[10];
    	if(m < 10)
    	{
    	    sprintf(buf, "%d:0%d",h,m);    
    	}
    	else
    	{
    	    sprintf(buf, "%d:%d",h,m);
    	}
    	
    	return string(buf);
    	
    }
    
    
    void readBWRec(int num, unsigned int time, int nxtV, vector<string> &res)
    {
    	if(!num)
    	{
    		string r = convertIntToTime(time);
    		if(r != "")
    		    res.push_back(r);
    		return;
    	}
    
    	unsigned int temp = time;
    	for(int i = nxtV; i < 10 - num + 1; i++)
    	{
    		time = temp;
    		time |= 1 << 21 + (10 - i);
    		readBWRec(num - 1, time, i + 1, res);
    	}			
    }
    
    vector<string> readBinaryWatch(int num) {
       unsigned int time = 0;
       vector<string> res;
       readBWRec(num, time, 0, res);
       return res;
    }
    

    };


Log in to reply
 

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