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

实际为10进制转26进制,但是需要注意的地方是如果取模为0的时候,需要向前借位,否则结果就不正确了。

代码:

public class Solution {
    public String convertToTitle(int n) {
        String result="";
        while(true){
            int num=n%26;
            if(num==0){
                num=26;
                n-=26;
            }
            char c=(char)(num-1+‘A‘);
            result=c+result;
            if(n<26){
                break;
            }
            n/=26;
        }
        return result;
    }
}
时间: 2024-08-09 06:33:26

Leet Code OJ 168. Excel Sheet Column Title [Difficulty: Easy]的相关文章

Leet Code OJ 119. Pascal&#39;s Triangle II [Difficulty: Easy]

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 翻译: 给定一个下标k,返回第k行的杨辉三角. 例如给定k=3,返回[1,3,3,1]. 提示:你可以优化你的算法,让它只使用O(k)的额

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

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.

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) { //resi

【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