Hi, this is Shunchi!

  • Home

  • Tags0

  • Archives267

  • Categories0

  • Curricula

  • DSA

  • LeetCode_Notes

  • Interviews

  • General

  • Resume

168. Excel Sheet Column Title

Posted on 2020-05-12 | Edited on 2021-01-22

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);
}
}
<1…203204205…267>
ShunchiZhou

ShunchiZhou

267 posts
RSS
GitHub E-Mail Gitbook Linkedin
© 2024 ShunchiZhou
Powered by Hexo v5.4.0
|
0%