【兩次過】Lintcode 1350. Excel表列標題
阿新 • • 發佈:2018-12-16
給定一個正整數,返回相應的列標題,如Excel表中所示。
樣例
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB
解題思路:
是1348. Excel Sheet Column Number的變形,本質仍然是一個進位制轉換問題,但是需要注意map中對應的是0-25, 而實際中是1-26,這中間的轉換需搞明白。
public class Solution { /** * @param n: a integer * @return: return a string */ public String convertToTitle(int n) { // write your code here HashMap<Integer, Character> map = new HashMap<>(); for(int i=0; i<26; i++) map.put(i, (char)('A'+i)); StringBuilder str = new StringBuilder(); while(n != 0){ str.insert(0,map.get((n-1)%26)); n = (n-1)/26; } return str.toString(); } }