标签:
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB
分析:
类比10进制数,此问题可简化为26进制数的问题(0-25)。
char* convertToTitle(int n) { int tmp = n; int i = 0; char *str = NULL; while(tmp > 0) { i++; tmp = (tmp-1)/26; } str = (char *) malloc(i*sizeof(char)); tmp = n; for(; i>0; i--) { str[i-1] = (tmp-1)%26+‘A‘; tmp =(tmp-1)/26; } return str; }
LeetCode 168. Excel Sheet Column Title
标签:
原文地址:http://www.cnblogs.com/twozc/p/5221878.html