leetcodeノート:Excel Sheet Column Number
一.タイトルの説明
Given a column title as appear in an Excel sheet, return its corresponding column number.
For example:
二.テーマ分析
この問題は、Excelテーブルに表示される列ヘッダーを指定し、対応する列番号を返すことを意味します.実は、文字列から整数への変換を実現する関数を作成します.
試験用例を見てみると、問題は
三.サンプルコード
四.小結
この問題はただ1本の進数変換の基礎の問題で、水の問題..
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
二.テーマ分析
この問題は、Excelテーブルに表示される列ヘッダーを指定し、対応する列番号を返すことを意味します.実は、文字列から整数への変換を実現する関数を作成します.
試験用例を見てみると、問題は
A ~ Z
表示1 ~ 26
AA
キャリーあり、整数27
であるため、問題は実際に26
進数を10
進数に変換している.三.サンプルコード
C++
class Solution {
public:
int titleToNumber(string s) {
int n = s.length(), result = 0;
if (n == 0) return result;
for (int i = 0; i < n; ++i)
result = result * 26 + (s[i] - 'A' + 1);
return result;
}
};
// Python
class Solution:
# @param s, a string
# @return an integer
def titleToNumber(self, s):
result = 0
for c in s:
result = result * 26 + (ord(c) - ord('A') + 1)
return result
四.小結
この問題はただ1本の進数変換の基礎の問題で、水の問題..