美文网首页ACM题库~
LeetCode 168. Excel Sheet Column

LeetCode 168. Excel Sheet Column

作者: 关玮琳linSir | 来源:发表于2017-10-23 14:17 被阅读8次

    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](https://leetcode.com/discuss/user/ifanchu) for adding this problem and creating all test cases.
    

    题意:把数字转换成字母,就好像是一个进制的转换问题。

    java代码:

    class Solution {
        public String convertToTitle(int n) {
            StringBuilder builder = new StringBuilder();
            for (; n != 0; n = (n - 1) / 26) {
                char c = (char)(n % 26 + 64);
                if (c == 64) c = 90;
                builder.append(c);
            }
            builder.reverse();
            return new String(builder);
        }
    }
    

    相关文章

      网友评论

        本文标题:LeetCode 168. Excel Sheet Column

        本文链接:https://www.haomeiwen.com/subject/owcauxtx.html