168. Excel Sheet Column Title
一、题目
1、审题
2、分析
给出一个正整数,给出如上对应方式对应的字符串。
二、解答
1、思路:
sheet 中,1 —— 26 对应 A——Z;
而, A——Z 对应的下标为 0——25;
所以 每次计算时 n--;
/* * I think the reason we do n-- is that number 0 is not included in this sheet. * We can consider this transformation as base-26. However, classic base-26 consists of * number from 0 to 25, and in this case we have numbers from 1 to 26. Now n-- seems intuitive and reasonable. */ public String convertToTitle2(int n) { StringBuilder sb = new StringBuilder(); while(n > 0) { n--; sb.append((char)('A' + n % 26)); // 'A' 自带了 1 个单位 n /= 26; } return sb.reverse().toString(); }