C code.very easy no extra memory.0 ms solution


  • 0
    S
    
    int count_ones(int n)
    {
        int count=0;
        while(n)
        {
            n = n & (n-1);
            count++;
        }
        return count;
    }
    
    char** readBinaryWatch(int num, int* returnSize)
    {
        char **ans = malloc(sizeof(char *) * 1024);
        int ret_count=0;
            
        for(int h=0;h<12;h++)
        {
            for(int m=0;m<60;m++)
            {
                int tmp = (h<<6)|m;
                if(count_ones(tmp)==num)
                {                
                    char *tmp = malloc(sizeof(char)*6);
                    sprintf(tmp,"%d:%02d",h%12,m%60);
                    ans[ret_count++] = tmp;
                }
            }
        }
        *returnSize=ret_count;
        return ans;
    }
    

Log in to reply
 

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