43 - excel表列名称
Last updated
Was this helpful?
Last updated
Was this helpful?
给定一个正整数,返回它在 Excel 表中相对应的列名称。
例如,
1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB ...
示例 1:
输入: 1 输出: "A"
示例 2:
输入: 28 输出: "AB"
示例 3:
输入: 701 输出: "ZY"
求26进制的题。
作者:zjutszl
链接:
Runtime: 56 ms, faster than 41.18% of JavaScript online submissions for Excel Sheet Column Title.
Memory Usage: 34 MB, less than 25.00% of JavaScript online submissions for Excel Sheet Column Title.
这里把Z
放在了第一个,就避免了进位的麻烦
Runtime: 0 ms, faster than 100.00% of Go online submissions for Excel Sheet Column Title.
Memory Usage: 1.9 MB, less than 100.00% of Go online submissions for Excel Sheet Column Title.
???是没人写go吗?怎么这个最笨的方法居然能击败所有人,惊呆了
字母转数字是:'a'.charCodeAt()
。测出来A
是65,Z
是90。
因此可以把n
先加个64,就能拿到相应字母了。
作者:vailing
Runtime: 44 ms, faster than 96.96% of JavaScript online submissions for Excel Sheet Column Title.
Memory Usage: 34 MB, less than 14.29% of JavaScript online submissions for Excel Sheet Column Title.
另一种解法。先减,再加上65,就能绕开为余数是否为0的判断
Runtime: 52 ms, faster than 68.50% of JavaScript online submissions for Excel Sheet Column Title.
Memory Usage: 33.7 MB, less than 85.71% of JavaScript online submissions for Excel Sheet Column Title.
经测试,A
是65,Z
是90。和js一样,应该是同样用了unicode
。
换言之,之前用列表法反而麻烦了,还要再绕一下转成string
Runtime: 0 ms, faster than 100.00% of Go online submissions for Excel Sheet Column Title.
Memory Usage: 1.9 MB, less than 100.00% of Go online submissions for Excel Sheet Column Title.
Runtime: 0 ms, faster than 100.00% of Go online submissions for Excel Sheet Column Title.
Memory Usage: 2 MB, less than 100.00% of Go online submissions forExcel Sheet Column Title.
js中:String.fromCharCode(97)
。可以把数字转成字母。
链接:
go里面的字符,如A
,是存为rune
的,,类似于js里面'a'.charCodeAt()
转换出来的数字,大概是unicode
,可以通过这个数字找到这个字符。将其转换为string才能打印出来,否则打印出来的就是这个整数。