forked from codemistic/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode168.java
More file actions
39 lines (33 loc) · 934 Bytes
/
Leetcode168.java
File metadata and controls
39 lines (33 loc) · 934 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.company;
public class Leetcode168 {
public static void main(String[] args) {
int columnNumber=701;
String answer=convertToTitle(columnNumber);
System.out.println(answer);
}
public static String convertToTitle(int columnNumber) {
int n=columnNumber,x=0,y=0;
String columnTitle="";
while (n>=1){
x=n/26;
if (x==0){
y=n+64;
columnTitle=columnTitle+(char)y;
n=n/26;
}
if (x>=1 && x<=26){
y=x+64;
columnTitle=columnTitle+(char)y;
break;
}
if (x>26){
y=x%26;
y=y+64;
columnTitle=columnTitle+(char)y;
n=n/26;
}
}
columnTitle=columnTitle+(char)((columnNumber%26)+64);
return columnTitle;
}
}