leetcode 168. Excel表列名称

链接: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"
class Solution {
public:
    string convertToTitle(int n) {
        string a="";
        while(n){
            n--;
            char b=n%26;
            b=b+‘A‘;
            a=a+b;
            n/=26;
        }
        for(int i=0;i<a.length()/2;i++){
            swap(a[i],a[a.length()-1-i]);
        }

        return a;
    }
};

原文地址:https://www.cnblogs.com/wz-archer/p/12533509.html

时间: 2024-07-30 13:22:28

leetcode 168. Excel表列名称的相关文章

[LeetCode] Excel Sheet Column Title 求Excel表列名称

Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB Credits:Special thanks to @ifanchu for adding this problem and creating all test

168 Excel Sheet Column Title Excel表列名称

给定一个正整数,返回它在Excel表中相对应的列名称.示例:    1 -> A    2 -> B    3 -> C    ...    26 -> Z    27 -> AA    28 -> AB 详见:https://leetcode.com/problems/excel-sheet-column-title/description/ class Solution { public: string convertToTitle(int n) { string

【leetcode 简单】第三十八题 Excel表列名称

给定一个正整数,返回它在 Excel 表中相对应的列名称. 例如, 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB ... 示例 1: 输入: 1 输出: "A" 示例 2: 输入: 28 输出: "AB" 示例 3: 输入: 701 输出: "ZY" class Solution(object): def convertToTitle(self, n):

3. 无重复字符的最长子串 141. 环形链表 171. Excel表列序号 203. 移除链表元素

3. 无重复字符的最长子串 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3.示例 2: 输入: "bbbbb"输出: 1解释: 因为无重复字符的最长子串是 "b",所以其长度为 1.示例 3: 输入: "pwwkew"输出: 3解释: 因为无重复字符的最长子串是 "

【leetcode 简单】第四十题 Excel表列序号

给定一个Excel表格中的列名称,返回其相应的列序号. 例如, A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ... 示例 1: 输入: "A" 输出: 1 示例 2: 输入: "AB" 输出: 28 示例 3: 输入: "ZY" 输出: 701 class Solution: def titleToNumber(self, s): ""

Leetcode 168 Excel Sheet Column Title 进制数转化

题意:将数字转化成excel表中的行中的项目 本质是10进制转化为26进制,但是在中间加入了一个不一样的操作,在每次操作前都需要n-- 1 class Solution { 2 public: 3 string convertToTitle(int n) { 4 string s(""); 5 for(; n--; n/=26){ 6 s += string("A"); 7 s[s.size() - 1] += n% 26; 8 } 9 reverse(s.begi

[LeetCode] Excel Sheet Column Number 求Excel表列序号

Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 Credits:Special thanks to @ts for addi

Leetcode 168 Excel Sheet Column Title

Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB Credits:Special thanks to @ifanchu for adding this problem and creating all test

Java for LeetCode 168 Excel Sheet Column Title

Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: 1 -> A    2 -> B    3 -> C    ...    26 -> Z    27 -> AA    28 -> AB 解题思路: JAVA实现如下: static public String convertToTitle(int n) { S