Beats 100% for most input cases, C++ with explanation


  • 0
    E

    Solution:
    Cosider A,B,C.... Z as 1,2,3.... 26
    AB => A * 26^1 + B * 26^0 => 26 + 2 => 28
    AAC => A * 26^2 + A * 26^1 + C * 26^ 0 => 705
    "class Solution {
    public:
    int titleToNumber(string s) {
    int N = s.size();
    if(!N) return 0;
    int val = 1;
    int result = 0;
    for(int i=N-1;i>=0;i--)
    {
    result = result + ((s[i]-65+1)*val);
    val = val * 26;
    }
    return result;
    }
    };"


Log in to reply
 

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