Share my accepted Java solution


  • 0
    S

    The idea is that the base of each digit increase by the factor of 26.

    public class Solution {
        public int titleToNumber(String s) {
            int result = 0;
            if (s.length() == 0)
                return result;
                
            int base = 1;
            for (int d = s.length() - 1; d >= 0; d--)   {
                int dVal = (int)(s.charAt(d)) - (int)'A' + 1; // 'A': 1, ...
                result += dVal * base;
                base *= 26;            
            }
            
            return result;
        }
    }

  • 0
    N

    how long did it run?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.