当前位置:网站首页>Excel sheet column title for leetcode Title Resolution

Excel sheet column title for leetcode Title Resolution

2022-06-23 05:50:00 ruochen

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 
    public String convertToTitle(int n) {
        if (n < 27) {
            return (char) ('A' + (n - 1)) + "";
        }
        if (n % 26 == 0) {
            return convertToTitle(n / 26 - 1) + 'Z';
        }
        return convertToTitle(n / 26) + convertToTitle(n % 26);
    }
原网站

版权声明
本文为[ruochen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/01/202201151252028765.html