[LeetCode练习题-C语言]168. Excel Sheet Column Title

来源:互联网 发布:护眼仪有用吗 知乎 编辑:程序博客网 时间:2024/05/22 16:53

[LeetCode练习题-C语言]168. Excel Sheet Column Title

题目

Given a positive integer, return its corresponding column title as appear in an Excel sheet.

For example:

1 -> A2 -> B3 -> C...26 -> Z27 -> AA28 -> AB 

我的代码

char* convertToTitle(int n) {    char *result ;    int j,temp,i =0;    while(n > 0){        char c;        if(n%26 == 0){            c='Z';            n=(n-26)/26;        }        else{            c = n%26 -1 +'A';            n=(n-n%26)/26;        }        result[i]= c;        i++;    }    //由于leetcode默认返回值为输入值,所以加以下两句,不然返回结果后几位会为输入的后几位    int len = strlen(result);    *(result+i) = *(result+len);    for(j=0; j < i/2; j++){        temp = result[j];        result[j] = result[i-j-1];        result[i-j-1] = temp;    }    return result;}
0 0
原创粉丝点击