1
+ package easy ;
2
+ /**168. Excel Sheet Column Title QuestionEditorial Solution My Submissions
3
+ Total Accepted: 70066
4
+ Total Submissions: 306198
5
+ Difficulty: Easy
6
+ Given a positive integer, return its corresponding column title as appear in an Excel sheet.
7
+
8
+ For example:
9
+
10
+ 1 -> A
11
+ 2 -> B
12
+ 3 -> C
13
+ ...
14
+ 26 -> Z
15
+ 27 -> AA
16
+ 28 -> AB */
17
+ public class ExcelSheetColumnTitle {
18
+ public String convertToTitle_accepted (int n ) {
19
+ /**'Z' is the corner case, so we'll have to special case handling specially, also, we'll have to do (n-1)/26,
20
+ * only when this is not equal to 1, we'll continue.*/
21
+ StringBuilder sb = new StringBuilder ();
22
+ while (n != 0 ){
23
+ int temp = n %26 ;
24
+ if (temp == 0 ) sb .append ("Z" );
25
+ else sb .append ((char )(temp +64 ));
26
+ n = (n -1 )/26 ;
27
+ }
28
+ return sb .reverse ().toString ();
29
+ }
30
+
31
+ public String convertToTitle_accepted_more_beautiful (int n ) {
32
+
33
+ StringBuilder sb = new StringBuilder ();
34
+ while (n != 0 ){
35
+ int temp = (n -1 )%26 ;
36
+ sb .append ((char )(temp +65 ));
37
+ n = (n -1 )/26 ;
38
+ }
39
+ return sb .reverse ().toString ();
40
+
41
+ }
42
+
43
+ public static void main (String ...strings ){
44
+ ExcelSheetColumnTitle test = new ExcelSheetColumnTitle ();
45
+ // int n = 28899;
46
+ // int n = 1;
47
+ // int n = 1000000001;
48
+ // int n = 26;
49
+ // int n = 27;
50
+ int n = 28 ;
51
+ // int n = 52;
52
+ // int n = 53;
53
+ // System.out.println((int) 'A');
54
+ // System.out.println(1000000001/26);
55
+ // System.out.println(25*26);
56
+ // System.out.println(26*26);
57
+ // System.out.println(27*26);
58
+ // System.out.println(702%26);
59
+ // System.out.println(702/26);
60
+ System .out .println (Integer .parseInt (String .valueOf (26 ), 10 ));
61
+ System .out .println (test .convertToTitle_accepted (n ));
62
+ }
63
+
64
+ }
0 commit comments