首页 > 试题广场 >

Excel 表列序号

[编程题]Excel 表列序号
  • 热度指数:1660 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
现给你一个仅由大写字母组成的字符串S,表示Excel表格里面的列名称。比如字符串A的序号为1,字符串B的序号为2,字符串Z的序号为26,字符串 AA的序号为27,请输出该字符串的序号。

S在字符串AXFD范围内
示例1

输入

"C"

输出

3

说明

字符串c的序号为3 
示例2

输入

"AB"

输出

28

说明

字符串ab的序号为28 
class Solution:
    def getNumber(self , S: str) -> int:
        # write code here
        res = 0
        S = S.upper()[::-1]
        for i in range(len(S)):
            res += (ord(S[i]) - 64) * (26 ** i)
        return res
发表于 2023-06-05 01:50:44 回复(0)
这道题本质是,将26个字母构成的序列视为26进制,然后转换成对应的10进制结果。
知道这个就能放弃了。这道题是浪费时间。
  1. 题目明确了用例字符串仅由大写字符构成,结果不通过的case全部是小写字符。
  2. 题干信息太少。单纯看A->1,B->2,Z->26,AA->27,AB->28,有多种实现可能性,并不能确定是26进制实现的,只能依赖不通过的case来补完。
#include <cctype>
class Solution {
public:
    int getNumber(string S) {
        // write code here
        string new_str = "";
        for (int i = 0; i < S.length(); i++) {
            new_str += toupper(S[i]);
        }
        int res = 0;
        for (int i = new_str.length() - 1; i >= 0; i--) {
            int weight = new_str.length() - 1 - i;
            res += (new_str[i] - 'A' + 1) * pow(26, weight);
        }
        return res;
    }
};


发表于 2023-05-14 21:59:19 回复(0)
package main
import "strings"

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param S string字符串 
 * @return int整型
*/
func getNumber( S string ) int {
    S=strings.ToUpper(S)
    ans:=0
    for _,ch:=range []byte(S){
        ans=ans*26+int(ch-'A')+1
    }
    return ans
}

发表于 2023-03-17 10:16:16 回复(0)
public class GetNumber {
	public int getNumber(String S) {
		// write code here
		S = S.toUpperCase(Locale.ROOT);
		char[] chars = S.toCharArray();
		int number = 0;
		for (int i = 0; i < chars.length; i++) {
			int num = chars[i] - 'A' + 1;
			number = number * 26 + num;
		}
		return number;
	}
}

发表于 2022-10-10 14:40:46 回复(0)
class Solution:
    def getNumber(self , S: str) -> int:
        # write code here
        import string
        res = 0
        for i, v in enumerate(S[::-1]):
            res += (string.ascii_uppercase.index(v)+1)*26**i
        return res

发表于 2022-07-06 22:19:50 回复(0)
import string
class Solution:
    def getNumber(self , S: str) -> int:
        # write code here
        d = dict(zip(string.ascii_uppercase, range(1,27)))
        res = 0
        for i, ch in enumerate(S):
            res += d[ch]*26**(len(S)-1-i)
        return res

发表于 2022-04-22 16:02:00 回复(0)
import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param S string字符串 
     * @return int整型
     */
    public int getNumber(String S) {
        // write code here
        int result = 0;
        if (S == null || "".equals(S)) {
            return 0;
        }
        char[] chars = S.toCharArray();
        for (int i = chars.length - 1; i >= 0; i--) {
            int v = (int) ((chars[i] - 'A' + 1) * Math.pow(26, chars.length - 1 - i));
            result += v;
        }
        return result;
    }

}

发表于 2022-03-13 21:42:00 回复(0)
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param S string字符串 
# @return int整型
#
class Solution:
    def getNumber(self , S: str) -> int:
        # write code here
        d = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        n = 0
        i = 0
        S = S[::-1]
        for c in S:
            cn = d.index(c) + 1
            for j in range(i):
                cn = cn * 26
            n += cn
            i += 1
        return n
            

发表于 2022-03-10 10:32:37 回复(0)

问题信息

难度:
8条回答 2002浏览

热门推荐

通过挑战的用户

查看代码