-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathExcelColumnTile.java
More file actions
103 lines (59 loc) · 1.13 KB
/
ExcelColumnTile.java
File metadata and controls
103 lines (59 loc) · 1.13 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
Excel Column Title
Problem Description
Given a positive integer A, return its corresponding column title as appear in an Excel sheet.
For example:
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB
Problem Constraints
1 <= A <= 109
Input Format
First and only argument of input contains single integer A
Output Format
Return a string denoting the corresponding title.
Example Input
Input 1:
A = 3
Input 2:
A = 27
Example Output
Output 1:
"C"
Output 2:
"AA"
Example Explanation
Explanation 1:
3 corrseponds to C.
Explanation 2:
1 -> A,
2 -> B,
3 -> C,
...
26 -> Z,
27 -> AA,
28 -> AB
*/
/*
Solution Approach
Think of it like this.
How would you convert a number to binary ?
Can you apply the same principle here now that the base is different ?
*/
public class Solution {
public String convertToTitle(int A) {
String ans = "";
while(A>0){
int rem = (A-1)%26 + 1;
ans = (char)('A'+rem-1)+ans;
A /= 26;
if(rem==26)
A--;
}
return ans;
}
}