Code/LeetCode

[LeetCode] 171. Excel Sheet Column Number (Easy/Python)

코방코 2022. 11. 16. 23:04
728x90
 

Excel Sheet Column Number - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

Above is the link to the problem.

 

Problem

Given a string columnTitle that represents the column title as appears in an Excel sheet, return its corresponding column number.

 

Example 1

A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28 
...

 

Example 2

Input: columnTitle = "ZY"
Output: 701

 

Analysis

There is 26 alphabet from A to Z.

We calculate A as 1, B as 2 ... Z as 26.

Count digit numbers starting with 0 from the rightmost.

That digit number will be an exponential of 26 and multiply each digit alphabet.

The maximum number of digit number will be the length of columnTitle - 1

 

I solved this problem by the below code.

 

This code gets the top 16.3% of time complexity and uses 13.9 MB memory.

 

 

728x90
반응형