Leetcode Excel Sheet Column Number (C++) && Excel Sheet Column Title ( Python)
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
1 class Solution { 2 public: 3 int titleToNumber(string s) { 4 int number = 0; 5 int tmp = 0; 6 for (int i = 0; i < s.length();i++){ 7 tmp = s[i] - 'A' + 1; 8 number = number * 26 + tmp; 9 } 10 return number; 11 12 } 13 };
Excel Sheet Column Title
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:
1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB
主要思路:10进制转成26进制。但需要注意先减1,因为如52 = 2*(26^1),若不减1,则为B*,而不是正确结果AZ。所以为了避免这种整除的情况,事先减一。
class Solution: # @return a string def convertToTitle(self, num): res = '' while num > 0: tmp = num num = (tmp-1)/26 res += chr(65+(tmp-1)%26) return res[::-1]
chr(i):返回整数i对应的ASCII字符。与ord()作用相反。
参数x:取值范围[0, 255]之间的正数。
版本:该函数在python2和python3各个版本中都可用。不存在兼容性问题。
'A'对应的数字为65