This article is participating in Python Theme Month. See the link to the event for more details
Topic describes
This is the 171. Excel table number in LeetCode, difficulty is simple.
Tag: “analog”, “base conversion”
You are given the string columnTitle, which represents the column name in an Excel table. Returns the column ordinal of the column name.
For example,
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
Copy the code
Example 1:
Input: columnTitle = "A" Output: 1Copy the code
Example 2:
Input: columnTitle = "AB" Output: 28Copy the code
Example 3:
Input: columnTitle = "ZY" Output: 701Copy the code
Example 4:
Input: columnTitle = "FXSHRXW" Output: 2147483647Copy the code
Tip:
- 1 <= columnTitle.length <= 7
- ColumnTitle Consists of only uppercase English
- ColumnTitle is in the range [“A”, “FXSHRXW”]
Hexadecimal conversion
You may not have done the “base conversion” class, but you do use the following “base conversion” method in base 101010.
If the topic is base 101010 conversion, then you can easily imagine the following conversion process: From high to low, start with ansansans being 000, and update ansansans with the current digit each time, as ans= ANS ∗10+ Valians = ANS * 10+ val_IANS = ANS ∗10+vali.
Take 🌰 as an example. Suppose there is a decimal number encoded as ABCDABCDABCD. The conversion process is as follows:
= 0
Ans = ANS ∗10+ 1ANS = ANS * 10+ 1ANS = ANS ∗10+1 => A
Ans = ANS ∗10+ 2ANS = ANS * 10+ 2ANS = ANS ∗10+2 => B
Ans = ANS ∗10+ 3ANS = ANS * 10+ 3ANS = ANS ∗10+3 => C
Ans = ANS ∗10+ 4ANS = ANS * 10+ 4ANS = ANS ∗10+4 => D
In this case, the base 101010 is replaced with base 262626.
Java code:
class Solution {
public int titleToNumber(String s) {
char[] cs = s.toCharArray();
int n = cs.length;
int ans = 0;
for (int i = 0; i < n; i++) {
ans = ans * 26 + (cs[i] - 'A' + 1);
}
returnans; }}Copy the code
Python 3 code:
class Solution:
def titleToNumber(self, columnTitle: str) - >int:
ans = 0
for s in columnTitle:
ans = ans * 26 + (ord(s) - ord('A') + 1)
return ans
Copy the code
- Time complexity: O(n)O(n)O(n)
- Spatial complexity: because
toCharArray
Creates ands
Array of equal length, so usecharAt
Instead oftoCharArray
The word of the Lord as
, or for
expand
You can review them together
- Excel column name: Problem solving
The last
This is No.171 in our “Brush through LeetCode” series, which began on 2021/01/01. As of the start date, there are 1916 questions on LeetCode, some with locks, and we will first brush through all the questions without locks.
In this series of articles, in addition to explaining how to solve the problem, I’ll give you the most concise code possible. If the general solution is involved, the corresponding code template will be provided.
In order to facilitate the students to debug and submit the code on the computer, I set up a related warehouse: github.com/SharingSour…
In the repository address, you can see the solution to the series, the corresponding code to the series, the LeetCode link, and other preferred solutions to the series.