Example 1: Input: "A" Output: 1 Example 2: Input: "AB" Output: 28 Example 3: Input: "ZY" Output: 701
Formula: 26^len * char + 26^len-1 * char + 26^len-2 * char + .......... + 26^0*char
26^len * char + 26^len-1 * char + 26^len-2 * char + .......... + 26^0*char
class Solution { public: int titleToNumber(string s) { int l = s.length() - 1; int res = 0; for (int i = 0; i < s.length(); i++) { int x = pow(26, l) * (s[i] - 64); l--; res = res + x; } return res; } };
Last updated 4 years ago