ACM题库~

LeetCode 168. Excel Sheet Column

2017-10-23  本文已影响8人  关玮琳linSir

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);
    }
}
上一篇下一篇

猜你喜欢

热点阅读