领扣LintCode算法问题答案-1350. Excel表列标题

    科技2022-07-13  181

    领扣LintCode算法问题答案-1350. Excel表列标题

    目录

    1350. Excel表列标题描述样例 1:样例 2: 题解鸣谢

    1350. Excel表列标题

    描述

    给定一个正整数,返回相应的列标题,如Excel表中所示。

    1 -> A 2 -> B 3 -> C … 26 -> Z 27 -> AA 28 -> AB

    样例 1:

    输入: 28 输出: "AB"

    样例 2:

    输入: 29 输出: "AC"

    题解

    public class Solution { /** * @param n: a integer * @return: return a string */ public String convertToTitle(int n) { // write your code here StringBuilder sb = new StringBuilder(); int mode = n; while (n > 26) { mode = (n - 1) % 26; n = (n - 1) / 26; sb.append((char)('A' + mode)); } sb.append((char)('A' + n - 1)); return sb.reverse().toString(); } }

    原题链接点这里

    鸣谢

    非常感谢你愿意花时间阅读本文章,本人水平有限,如果有什么说的不对的地方,请指正。 欢迎各位留言讨论,希望小伙伴们都能每天进步一点点。

    Processed: 0.013, SQL: 8