168. Excel Sheet Column Title (Math)

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

class Solution {
public:
    string convertToTitle(int n) {
        //residue is the value of current bit (from least-significant bit)
        //divided by 26, then iterate
        int residue = n%26, quotient=n;
        string ret = "";

        while(quotient>0){
            if(residue==0) {
                residue = 26;
                quotient -= 1;
            }
            ret = (char)(‘A‘+residue-1) + ret;

            quotient /= 26;
            residue = quotient %26;
        }

        return ret;
    }
};
时间: 2024-10-11 01:40:56

168. Excel Sheet Column Title (Math)的相关文章

168.Excel Sheet Column Title

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 注意:由于下标从1开始而不是从0开始,因此要减一操作. static public string Convert

Leet Code OJ 168. Excel Sheet Column Title [Difficulty: Easy]

题目: 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 翻译: 给定一个正数,返回它类似Excle中对应的列标题. 分析: 关联问题:"Excel Sheet Column Number"

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

【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 Solution: 由于A->1,  计算s的末尾时需先减去1 1 class Solution { 2 public: 3 string convert

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

168. Excel Sheet Column Title (Easy)

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 思路:1.进制转化,将十进制转化为每位以A-Z表示的26进制数:2.Python中,使用ord()函数将字母转化为整数,使用chr()函数将整数转化回字母:3.

【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 简单题 class Solution { public: string convertToTitle(int n) { string str; char

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

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