您的位置:首页 > 编程语言 > C语言/C++

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

2016-07-27 13:07 489 查看

[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 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言 leetcode