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

链接:https://leetcode-cn.com/problems/two-sum/solution/javascript-shao-you-bu-tong-de-26jin-zhi-by-zjutsz/

var convertToTitle = function (n) {
  const data = ['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']
  let ans = ""
  while (n) {
    ans = data[n % 26] + ans
    n = n % 26 === 0 ? Math.floor(n / 26) - 1 : Math.floor(n / 26)
  }
  return ans
};

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

func convertToTitle(n int) string {
    data := []rune{'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'}
    var ans string
    for n != 0 {
        ans = string(data[n%26]) + ans
        if n%26 == 0 {
            n = (n / 26) - 1
        } else {
            n = n / 26
        }
    }
    return ans
}

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/

var convertToTitle = function (n) {
  let result = ""
  temp = 0
  while (n) {
    temp = n % 26
    if (temp === 0) {
      result = 'Z' + result
      n--
    } else {
      result = String.fromCharCode(temp + 64) + result
    }
    n = Math.floor(n / 26)
  }
  return result
};

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的判断

var convertToTitle = function (n) {
  let result = ""
  temp = 0
  while (n) {
    n--
    temp = n % 26
    result = String.fromCharCode(temp + 65) + result
    n = Math.floor(n / 26)
  }
  return result
};

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

func convertToTitle(n int) string {
    var (
        result string
        temp   int
    )
    for n != 0 {
        temp = n % 26
        if temp == 0 {
            result = string('Z') + result
            n--
        } else {
            result = string(temp+64) + result
        }
        n = n / 26
    }
    return result
}

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.

func convertToTitle(n int) string {
    var (
        result string
        temp   int
    )
    for n != 0 {
        n--
        temp = n % 26
        result = string(temp + 65) + result
        n /= 26
    }
    return result
}

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?