Wrong code in C


  • 0
    C
    char *convertToTitle(int n) {
        char *s;
        int char_size = 0, num = n;
        while(n>0){
            num = (num-1)/26;
            char_size++;
        }
        s = (char*)malloc(char_size*sizeof(char));
    
        while(n>0){
            int m=(n-1)%26;
            int i = char_size -1;
            s[i--]=m+'A';
            n/=26;
            m=0;
        }
        return s;
    }
    

    gcc said: Time Limit Exceeded. don't really know why.......


  • 0
    J

    first while(n>0) is a dead loop


  • 0

    u r modifying num in the first loop, while the condition is checking on n.


Log in to reply
 

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