171. Excel Sheet Column Number

xiaoxiao2021-02-28  18

Related to question Excel Sheet Column Title

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

思路:26进制转换

代码1:

class Solution { public: int titleToNumber(string s) { int res=0; for(char ch:s){ char temp = ch-'A'+1; res = res*26 + temp; } return res; } };

代码2:

class Solution { public: int titleToNumber(string s) { int res=0; for(char ch:s){ char temp = ch-'A'+1; res = res*26 + temp; } return res; } };

代码3:

class Solution { public: int titleToNumber(string s) { int res = 0; for (char c:s) { res *= 26; res += c - 'A' + 1; } return res; } };

转载请注明原文地址: https://www.6miu.com/read-2600162.html

最新回复(0)