leetcode:Excel Sheet Column Title

来源:互联网 发布:u盘数据怎么恢复 编辑:程序博客网 时间:2024/05/18 19:21
public class Solution {    public String convertToTitle(int n) {        String ret = "";        n--;        while(n>=0)        {            int tmpNum = n%26;            char tmpChar = (char)(tmpNum+'A');            n = n/26;            ret = tmpChar + ret;            n--;        }        return ret;    }}



同样可以看做是26进制的问题,不断取余即可得到从低位到高位的各个值.....

需要注意的是余数为1对应的是"A",为了避免余数为0不好处理的情况,每一轮都要将当前的n减一

0 0