Skip to content

Latest commit

 

History

History
 
 

n0168. Excel Sheet Column Title

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

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"

解法

// Author: Netcan @ https://github.com/netcan/Leetcode-Rust
// Zhihu: https://www.zhihu.com/people/netcan

impl Solution {
    pub fn convert_to_title(mut n: i32) -> String {
        let mut title = String::new();
        while n != 0 {
            title = ((((n - 1) % 26) as u8 + 'A' as u8) as char).to_string() + &title;
            n = (n - 1) / 26;
        }
        title
    }
}