168. Excel Sheet Column Title

来源:互联网 发布:mac os x dmg镜像 编辑:程序博客网 时间:2024/06/05 09:31

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 class Solution {    public String convertToTitle(int n) {        String s = "";        while(n>0) {            s = (char)((n - 1) % 26 + 'A') + s; //因为1match的是A,所以要n-1            n = (n - 1) / 26; //数字转char只要加上(char)就好了        }        return s;    }}