Skip to content

Commit 70dea5f

Browse files
committed
int to AB..Z string
1 parent a2eacc2 commit 70dea5f

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

PuzzleCoding/src/IntToString.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* A:0, B:1, C:2, ... , AA:26, AB:27, ... , given an integer, return
3+
corresponding string
4+
*/
5+
public class IntToString {
6+
public static void main(String[] args){
7+
int n=52;
8+
System.out.println(intToString(n));
9+
}
10+
11+
public static String intToString(int n){
12+
StringBuilder s = new StringBuilder();
13+
n += 1; // b/c A:0
14+
15+
while (n > 0){
16+
if(n % 26 == 0){
17+
s.insert(0, "Z");
18+
n -= 26;
19+
}else {
20+
s.insert(0,(char)('A'+ n%26 -1));
21+
}
22+
n /=26;
23+
24+
}
25+
26+
return s.toString();
27+
}
28+
}

0 commit comments

Comments
 (0)