[LeetCode]171. Excel Sheet Column Number

26进制转10禁止

Posted by JinFei on March 27, 2020

题目描述

Given a column title as appear in an Excel sheet, return its corresponding column number.

For example:
  A -> 1
    B -> 2
    C -> 3
    ...
    Z -> 26
    AA -> 27
    AB -> 28 
    ...

Example1:

  Input: "A"
    Output: 1

Example2:

  Input: "AB"
    Output: 28

Example3:

  Input: "ZY"
    Output: 701

解题思路

  • 26进制转换 本身代表权值
    class Solution {
    public:
      int titleToNumber(string s) {
          long long res = 0;
          for(auto& c : s){
              res = res * 26 + c - 'A' + 1;
          }
          return res;
      }
    };