LeetCode/solutions/171. Excel Sheet Column Number.md
2019-09-13 23:08:41 +08:00

19 lines
652 B
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [171. Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number/description/)
# 思路
第[168题](https://leetcode.com/problems/excel-sheet-column-title/description/)是将10进制转换为26进制这题是将26进制转换为10进制.
将k进制数"abcd"转换为10进制数`res = d * k^0 + c * k^1 + b * k^2 + a * k^3`.
# C++
``` C++
class Solution {
public:
int titleToNumber(string s) {
int res = 0, multiplier = 1;
for(int i = s.size() - 1; i >= 0; i--){
res += (int)(s[i] - 'A' + 1) * multiplier;
multiplier *= 26;
}
return res;
}
};
```