168. Excel Sheet Column Title

https://leetcode.com/problems/excel-sheet-column-title/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// because String is an immutable type, 
// using StringBuilder -> 0ms; using String -> 7ms.
class Solution_1 {
public String convertToTitle(int n) {
StringBuilder res=new StringBuilder();
while(n>0){
n--;
res.insert(0,(char)('A'+n%26));
n/=26;
}
return res.toString();
}
}

// recursion method (keep rewriting the immutable String type)
class Solution_2 {
public String convertToTitle(int n) {
return n == 0 ? "" : convertToTitle(--n / 26) + (char)('A' + n % 26);
}
}
0%