My solution in C of this question


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

  • 0
    A

    I have a question about your code as follow:
    If you used the malloc function to get space, the value in the space is random data.
    So, if you don't initialize it,what will you get from *s at first step of loop?


  • 0
    S

    In "*(s+char_size-i) = (char)(n%fac +'A') + *s;" why you add *s in this line? Your code can pass the test. But after I delete "+ *s", the code can pass the test too. And the value of *s should be random until you initialize.


Log in to reply
 

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