马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
给你一个字符串 columnTitle ,表现 Excel 表格中的列名称。返回 该列名称对应的列序号 。
比方:
- A -> 1
- B -> 2
- C -> 3
- ...
- Z -> 26
- AA -> 27
- AB -> 28
- ...
复制代码
示例 1:
- <strong>输入:</strong> columnTitle = "A"
- <strong>输出:</strong> 1
复制代码 示例 2:
- <strong>输入: </strong>columnTitle = "AB"
- <strong>输出:</strong> 28
复制代码 示例 3:
- <strong>输入: </strong>columnTitle = "ZY"
- <strong>输出:</strong> 701
复制代码
提示:
- 1 <= columnTitle.length <= 7
- columnTitle 仅由大写英文组成
- columnTitle 在范围 ["A", "FXSHRXW"] 内
- public class Solution {
- //10进制成26
- public static int titleToNumber(String columnTitle) {
- //定义字母数组
- char[] arr=new char[]{'A','B','C','D','E','F','G','H',
- 'I','J','K','L','M','N','O','P',
- 'Q','R','S','T','U','V','W','X',
- 'Y','Z'};
- char[] arr2=columnTitle.toCharArray();//待处理字符串
- double sum=0;//结果
- int arrLen= arr2.length;//字符个数
- double cimi=0;//次密
- while (arrLen>0)
- {
- for (int i=0;i<26;i++)
- {
- if(arr[i]==arr2[arrLen-1])
- { sum=sum+(i+1)*Math.pow(26,cimi); //x*26^n+...+x*26^0
- cimi++;
- break;
- }
- }
- arrLen--;
- }
- return (int) sum;
- }
- public static void main(String[] args) {
- System.out.println(titleToNumber("FXSHRXW"));
- }
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |