LeetCode-Excel Sheet Column Number
Majority Element
##题目
####Excel Sheet Column Number
Related to question Excel Sheet Column Title
Given a column title as appear in an Excel sheet, return its corresponding column number.
For example:
A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28####Credits:
Special thanks to @ts for adding this problem and creating all test cases.
##解题思路
该题是Excel Sheet Column Title的翻转,给出字母序列得到对应的单元格数字,这个其实只要找到每一个字母对应的数字,然后乘以其所在的位置的26的次方即可,类似于将字符串数字转化为整数数字。
##算法代码
代码采用JAVA实现:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25public class Solution {
public int titleToNumber(String s) {
if(s==null ||s.length()==0)
return 0;
//用一个map存放26个字母对应的数字
HashMap<Character,Integer> map=new HashMap<Character,Integer>();
char ch='A';
int num=1;
map.put(ch,num);
while(num<26)
{
ch+=1;
num+=1;
map.put(ch,num);
}
int res=0;
for(int i=0;i<s.length();i++)
{
res=res*26+map.get(s.charAt(i));
}
return res;
}
}