Leetcode 168. Excel Sheet Column Title

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Excel Sheet Column Title

2. Solution

  • Version 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution:
def convertToTitle(self, n):
result = ''
mapping = [chr(65+i) for i in range(0, 26)]

while n:
remainder = n % 26
quotient = n // 26
result = mapping[remainder - 1] + result
if remainder == 0:
quotient -= 1
n = quotient

return result
  • Version 2
1
2
3
4
5
6
7
8
9
10
class Solution:
def convertToTitle(self, n):
result = ''
mapping = [chr(65+i) for i in range(0, 26)]
while n:
remainder = n % 26
n = (n - 1) // 26
result = mapping[remainder - 1] + result

return result
  • Version 3
1
2
3
4
5
6
7
8
class Solution:
def convertToTitle(self, n):
result = ''
mapping = [chr(65+i) for i in range(0, 26)]
while n:
result = mapping[n % 26 - 1] + result
n = (n - 1) // 26
return result

Reference

  1. https://leetcode.com/problems/excel-sheet-column-title/
如果有收获,可以请我喝杯咖啡!