Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LeetCode] 168. Excel Sheet Column Title #168

Open
grandyang opened this issue May 30, 2019 · 0 comments
Open

[LeetCode] 168. Excel Sheet Column Title #168

grandyang opened this issue May 30, 2019 · 0 comments

Comments

@grandyang
Copy link
Owner

grandyang commented May 30, 2019

 

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 
    ...

Example 1:

Input: 1
Output: "A"

Example 2:

Input: 28
Output: "AB"

Example 3:

Input: 701
Output: "ZY"

Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases.

 

此题和 Excel Sheet Column Number 是一起的,但是我在这题上花的时间远比上面一道多,其实原理都一样,就是一位一位的求,此题从低位往高位求,每进一位,则把原数缩小26倍,再对26取余,之后减去余数,再缩小26倍,以此类推,可以求出各个位置上的字母。最后只需将整个字符串翻转一下即可。我们先从最简单的来看,对于小于26的数字,那么我们只需要对26取余,然后减去1,加上字符A即可,但是对于26来说,如果还是这么做的话就会出现问题,因为对26取余是0,减去1后成为-1,加上字符A后,并不等于字符Z。所以对于能被26整除的数我们得分开处理,所以就分情况讨论一下吧,能整除26的,直接在结果res上加上字符Z,然后n自减去26;不能的话,就按照一般的处理,n要减去这个余数。之后n要自除以26,继续计算下去,代码如下:

 

解法一:

class Solution {
public:
    string convertToTitle(int n) {
        string res = "";
        while (n) {
            if (n % 26 == 0) {
                res += 'Z';
                n -= 26;
            } else {
                res += n % 26 - 1 + 'A';
                n -= n % 26;
            }
            n /= 26;
        }
        reverse(res.begin(), res.end());
        return res;
    }
};

 

然后我们可以对上面对方法进行下优化,合并if和else,写的更简洁一些。从上面的讲解中我们得知,会造成这种不便的原因是能被26整除的数字,无法得到字符Z。那么我们用一个小trick,比如对于26来说,我们先让n自减1,变成25,然后再对26取余,得到25,此时再加上字符A,就可以得到字符Z了。叼就叼在这对其他的不能整除26的数也是成立的,完美解决问题,参见代码如下:

 

解法二:

class Solution {
public:
    string convertToTitle(int n) {
        string res;
        while (n) {
            res += --n % 26 + 'A';
            n /= 26;
        }
        return string(res.rbegin(), res.rend());
    }
};

 

这道题还可以用递归来解,而且可以丧心病狂的压缩到一行代码来解:

 

解法三:

class Solution {
public:
    string convertToTitle(int n) {
        return n == 0 ? "" : convertToTitle(n / 26) + (char)(--n % 26 + 'A');
    }
};

 

类似题目:

Excel Sheet Column Number

 

参考资料:

https://leetcode.com/problems/excel-sheet-column-title/

https://leetcode.com/problems/excel-sheet-column-title/discuss/51399/Accepted-Java-solution

https://leetcode.com/problems/excel-sheet-column-title/discuss/51398/My-1-lines-code-in-Java-C%2B%2B-and-Python

https://leetcode.com/problems/excel-sheet-column-title/discuss/51421/Share-my-simple-solution-just-a-little-trick-to-handle-corner-case-26

 

LeetCode All in One 题目讲解汇总(持续更新中...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant