https://leetcode.com/problems/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 

解题思路:

Excel Sheet Column Number 的follow-up,像上一题提到的。26进制实际上是从0-25,而本题的数字是从1-26。所以计算的时候,需要将数字减一。

public class Solution {
    public String convertToTitle(int n) {
        String res = "";
        while(n > 0) {
            res = (char)('A' + (n - 1) % 26) + res;
            n = (n - 1) / 26;
        }
        return res;
    }
}

 

相关文章:

  • 2021-08-10
猜你喜欢
  • 2022-01-09
  • 2021-07-05
  • 2021-08-20
  • 2021-11-07
  • 2022-12-23
  • 2022-01-05
相关资源
相似解决方案