Skip to content

Commit 814ef50

Browse files
author
ChienkuChen
committed
Add 168
1 parent 9ba3e43 commit 814ef50

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package _168;
2+
3+
class Solution {
4+
public String convertToTitle(int n) {
5+
StringBuilder s = new StringBuilder();
6+
7+
while (n > 0) {
8+
int r = n % 26;
9+
r = (r == 0) ? 26 : r;
10+
String tmp = Character.toString((char) (r + 64));
11+
s.insert(0, tmp);
12+
n = (n - r) / 26;
13+
}
14+
15+
return s.toString();
16+
}
17+
18+
public static void main(String[] args) {
19+
System.out.println(new Solution().convertToTitle(703));
20+
System.out.println(new Solution().convertToTitle(52));
21+
System.out.println(new Solution().convertToTitle(1));
22+
System.out.println(new Solution().convertToTitle(26));
23+
System.out.println(new Solution().convertToTitle(28));
24+
System.out.println(new Solution().convertToTitle(701));
25+
System.out.println(new Solution().convertToTitle(800));
26+
}
27+
}

0 commit comments

Comments
 (0)