Above is the link to the problem.
Problem
Given an integer columnNumber, return its corresponding column title as it appears in an Excel sheet.
For example:
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
Example
Input: columnNumber = 701
Output: "ZY"
Analysis
1 to 26 are considered A to Z.
And columnNumber = 27 must print 'AA'
if we divide 27 by 26 and get the quotient and remainder, both are 1
Because of this, we print 27 to 'AA'
But there is one problem.
if we divide 26 by 26, then we get the quotient = 1 and remainder = 0.
then the program can't compute 0 here.
So error must occur.
We can solve this problem like this.
If the remainder gets 0, then the remainder be 26 and the quotient subtracts 1
For example, if columnNumber is 26, the original result is quotient = 1 and remainder = 0,
but in this excel column, we think of this as quotient = 0 and remainder = 26
This is more example when columnNumber = 701
I solved this problem by the below code.
ASCII code : A=65 ~ Z=90 in the decimal number.
This code gets the top 0.78% of time efficiency and uses 13.9 MB of memory.
'Code > LeetCode' 카테고리의 다른 글
[LeetCode] 171. Excel Sheet Column Number (Easy/Python) (0) | 2022.11.16 |
---|---|
[LeetCode] 169. Majority Element (Easy/Python) (0) | 2022.10.05 |
[LeetCode] 136. Single number (Easy/Python) (0) | 2022.09.26 |
[LeetCode] 125. Valid Palindrome (Easy/Python) (0) | 2022.09.25 |
[LeetCode] 121. Best Time to Buy and Sell Stock (Easy/Python) (1) | 2022.09.23 |