Excel Sheet Column Number ( LeetCode ) Problem Solution.
For example:
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
Example 1:
Input: "A"
Output: 1
Example 2:
Input: "AB"
Output: 28
Example 3:
Input: "ZY"
Output: 701
Solution :-
class Solution:
def titleToNumber(self, s: str) -> int:
ans=0
for i in range(0,len(s)):
temp=ord(s[i])-64
ans+=temp*26**(len(s)-1-i)
return ans
Runtime: 28 ms, faster than 80.96% of Python3 online submissions for Excel Sheet Column Number.
Memory Usage: 12 MB, less than 8.06% of Python3 online submissions for Excel Sheet Column Number.
0 Comments