美文网首页算法每日一刷
LeetCode-168. Excel表列名称(Swift)

LeetCode-168. Excel表列名称(Swift)

作者: entre_los_dos | 来源:发表于2019-07-13 23:21 被阅读0次

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/excel-sheet-column-title

题目

给定一个正整数,返回它在 Excel 表中相对应的列名称。

例如,

    1 -> A
    2 -> B
    3 -> C
    ...
    26 -> Z
    27 -> AA
    28 -> AB 
    ...

示例 1:

输入: 1
输出: "A"

示例 2:

输入: 28
输出: "AB"

示例 3:

输入: 701
输出: "ZY"

26个一组,有26进制的感觉 。

方法

func convertToTitle(_ n: Int) -> String {
        
        //算出A的ASCII数值
        var ACode:Int = Int()
        for code in "A".unicodeScalars {
            ACode = Int(code.value)
        }
        var resultStr:String = String()
        
        var firstIndex = n
        

        while firstIndex > 0 {
            firstIndex = firstIndex-1
            let mod = firstIndex % 26
        
            let firstStr = Character(UnicodeScalar(ACode + mod)!)
                
            resultStr.append(firstStr)
            firstIndex = firstIndex / 26

        }
        return String(resultStr.reversed())
        

image.png

相关文章

  • LeetCode-168. Excel表列名称(Swift)

    来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/excel-...

  • Excel 表列序号、Excel表列名称

    给你一个字符串 columnTitle,表示 Excel 表格中的列名称。返回该列名称对应的列序号。 例如 示例 ...

  • Excel表列名称

    给定一个正整数,返回它在 Excel 表中相对应的列名称。 例如,1 -> A2 -> B3 -> C...26 ...

  • Excel表列名称

    题目 难度级别:简单 给定一个正整数,返回它在 Excel 表中相对应的列名称。 例如, 示例 1: 输入: 1输...

  • Python算法-进制转换

    168. Excel表列名称[https://leetcode-cn.com/problems/excel-she...

  • LeetCode.168 & 171 Excel

    168. Excel表列名称 171. Excel表列序号 168:看似是一个进制转换的问题,但是要注意用的是字符...

  • 168. Excel Sheet Column Title

    excel表列名称,给定整数 columnNumber,返回在 Excel表中相对应的列名称。 时间复杂度 O(N...

  • [LeetCode]168. Excel表列名称

    168. Excel表列名称给定一个正整数,返回它在 Excel 表中相对应的列名称。例如,1 -> A2 -> ...

  • 【leetcode】Excel表列序号

    【leetcode】Excel表列序号 给定一个Excel表格中的列名称,返回其相应的列序号。 例如, 示例 1:...

  • 38Excel表列名称

    给定一个正整数,返回它在 Excel 表中相对应的列名称。 例如,1 -> A2 -> B3 -> C...26 ...

网友评论

    本文标题:LeetCode-168. Excel表列名称(Swift)

    本文链接:https://www.haomeiwen.com/subject/ubdvkctx.html