Advertisement
sanya5791

Leetcode: 168. Excel Sheet Column Title

Jul 1st, 2023
1,387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.56 KB | None | 0 0
  1. class Solution {
  2.     fun convertToTitle(columnNumber: Int): String {
  3.         val aCode = 64
  4.         val dimentionNumber = 26
  5.  
  6.         val result = StringBuilder()
  7.         var num = columnNumber
  8.         while (num > 0) {
  9.             val remainder = if (num % dimentionNumber == 0) dimentionNumber
  10.                             else num % dimentionNumber
  11.             val ch = (remainder + aCode).toChar()
  12.             result.insert(0, ch)
  13.  
  14.             num -= remainder
  15.             num /= dimentionNumber
  16.         }
  17.  
  18.         return result.toString()
  19.     }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement