C 3ms


  • 0
    H
    char* optimalDivision(int* nums, int numsSize) 
    {
        char *s = calloc(100 , sizeof(char));
        char buf[5];
        short b = 0;
        
        sprintf(s,"%d", nums[0]);
        for(int i = 1; i < numsSize; ++i)
        {
            strcat(s,"/");
            if(nums[i] > 1)
            {
                if(!b && i + 1 < numsSize)
                {
                    strcat(s, "(");
                    b = 1;
                }
            }
            else
            {
                if(b)
                {
                    strcat(s, ")" );
                    b = 0;
                }
            }
            sprintf(buf, "%d", nums[i]);
            strcat(s,buf);
        }
        if(b)
            strcat(s, ")");
        return s;
    }
    
    

Log in to reply
 

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