forked from realpacific/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExcelSheetColumnTitle.kt
36 lines (32 loc) · 1.04 KB
/
ExcelSheetColumnTitle.kt
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
package questions
import _utils.UseCommentAsDocumentation
import utils.shouldBe
/**
* Given an integer columnNumber, return its corresponding column title as it appears in an Excel sheet.
* `A -> 1, B -> 2, C -> 3, Z -> 26, AA -> 27, AB -> 28`
*
* [Source](https://leetcode.com/problems/excel-sheet-column-title/)
*/
@UseCommentAsDocumentation
private fun convertToTitle(columnNumber: Int): String {
val characters = CharArray(26) {
(it + 65).toChar()
}
if (columnNumber <= 26) {
return characters[columnNumber - 1].toString()
}
var remaining = columnNumber
var result = ""
while (remaining > 0) {
result += characters[(remaining - 1) % 26]
remaining = (remaining - 1) / 26
}
return result.reversed()
}
fun main() {
convertToTitle(columnNumber = 1) shouldBe "A"
convertToTitle(columnNumber = 28) shouldBe "AB"
convertToTitle(columnNumber = 701) shouldBe "ZY"
convertToTitle(columnNumber = 702) shouldBe "ZZ"
convertToTitle(columnNumber = 2147483647) shouldBe "FXSHRXW"
}