现在的位置: 首页 > 综合 > 正文

LeetCode168——Excel Sheet Column Title

2017年11月08日 ⁄ 综合 ⁄ 共 509字 ⁄ 字号 评论关闭

LeetCode168——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 

Credits: Special thanks to @ifanchu for adding this problem and creating all test cases.

string convertToTitle(int n) {

}

题目大意

给定一个正整数,返回在一个电子表中对应栏目的标题。相关题目:LeetCode171——Excel Sheet Column Number

难度系数:容易

实现

string convertToTitle(int n) {
    n = n -1;
    int a = n / 26;
    if (a) {
        return convertToTitle(a) + (char)('A' + n % 26);
    } else {
        return string("")+(char)('A' + n % 26);
    }
}

这其实就是一个关于10进制转26进制的问题。这代码也不复杂,运行效率也不错。

抱歉!评论已关闭.