Excel Sheet Column Number

One of the favorite problems in LC because the problem looks simple but the solution involves quite a bit of analysis.

class Solution {
    fun titleToNumber(s: String): Int {
        var multiplier = 0        
        var sum = 0
        (s.length - 1 downTo 0).forEach {
            sum += ((s[it] - 'A') + 1) * Math.pow(26.0, multiplier.toDouble()).toInt()
            multiplier++
        }
        
        return sum
    }
}
Runtime: 148 ms, faster than 85.71% of Kotlin online submissions for Excel Sheet Column Number.
Memory Usage: 32.8 MB, less than 100.00% of Kotlin online submissions for Excel Sheet Column Number.
comments powered by Disqus