43 - excel表列名称
题目
给定一个正整数,返回它在 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放在了第一个,就避免了进位的麻烦
go
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吗?怎么这个最笨的方法居然能击败所有人,惊呆了
数字转字母
js中有这个方法:String.fromCharCode(97)。可以把数字转成字母。
字母转数字是:'a'.charCodeAt()。测出来A是65,Z是90。
因此可以把n先加个64,就能拿到相应字母了。
作者:vailing
链接:https://leetcode-cn.com/problems/two-sum/solution/wei-shi-yao-yao-jian-yi-by-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.
go
go里面的字符,如A,是存为rune的,rune实际是int32,类似于js里面'a'.charCodeAt()转换出来的数字,大概是unicode,可以通过这个数字找到这个字符。将其转换为string才能打印出来,否则打印出来的就是这个整数。
经测试,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.
Last updated
Was this helpful?