excel列序号与列名称的映射关系是:
1->A
1->A
2->B
...
27->AA
28->AB
...
数据范围:
package main //import "fmt" /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return string字符串 */ func ExcelTitle( n int ) string { ans:="" for n>0{ x:=n%26 if x==0{ ans=string('Z')+ans n=n/26-1 }else{ ans=string('A'+x-1)+ans n/=26 } } return ans }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param n int整型 # @return string字符串 # class Solution: def ExcelTitle(self, n: int) -> str: # write code here convertStr = "" convertDict = { 1: "A", 2: "B", 3: "C", 4: "D", 5: "E", 6: "F", 7: "G", 8: "H", 9: "I", 10: "J", 11: "K", 12: "L", 13: "M", 14: "N", 15: "O", 16: "P", 17: "Q", 18: "R", 19: "S", 20: "T", 21: "U", 22: "V", 23: "W", 24: "X", 25: "Y", 0: "Z", } while int(n / 26) != 0: x = convertDict[n % 26] # 获取末尾数字对应的字母 convertStr += x # 将对应的字母添加到临时变量末尾 if x == "Z": # 如果末尾是Z,因为对前一位借位,右移时要减去1 n = int((n / 26) - 1) else: n = int(n / 26) # 右移一位,使右二位移至右一位,以此类推 x = convertDict[n] convertStr += x return convertStr[::-1]
function ExcelTitle(n) { let v = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; let z = 0; let a = ""; while (n >= 1) { if (n % 26 !== 0) { a = v[(n % 26) - 1] + a; n = (n - (n % 26)) / 26; } else { a = v[25] + a; n = n / 26 - 1; } } return a; } module.exports = { ExcelTitle: ExcelTitle, };
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return string字符串 */ public String ExcelTitle(int n) { char[] chars = new char[] { 'Z', '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' }; // write code here StringBuilder sb = new StringBuilder(); while (n > 0) { int temp = n % 26; sb.append(chars[temp]); n = n / 26; if (temp == 0) { n = n - 1; } } return sb.reverse().toString(); } }