Skip to content

Commit

Permalink
ord and enumerate. lower is cool :love:
Browse files Browse the repository at this point in the history
  • Loading branch information
kantarcise authored Nov 11, 2023
1 parent aba717b commit ef65ca3
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Leet_Code/easy/171_excel_sheet_column_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# my first approach
class Solution:
def titleToNumber(self, columnTitle: str) -> int:
result = 0

j = 1
for i in range(len(columnTitle)-1, -1, -1):
# result = ord(f"{columnTitle[i].lower()}") * j
result += (ord(columnTitle[i].lower()) - ord('a') + 1) * j
j *= 26

return result

# fast and clean
class Solution:
def titleToNumber(self, columnTitle: str) -> int:
out = 0
for i, x in enumerate(columnTitle[::-1]):
out += (ord(x) - 64) * 26**i

return out

0 comments on commit ef65ca3

Please sign in to comment.