leetcode 154: 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 cases.

public class Solution {
    public String convertToTitle(int n) {
        //if n<=0 return empty string;
        StringBuilder sb = new StringBuilder();

        while(n>0) {
            int x = n % 26;
            if(x == 0) {
                sb.append('Z');
                --n;
            } else {
                sb.append((char)('A'-1+x));
            }
            n /= 26;
        }
        return sb.reverse().toString();
    }
}
时间: 2024-11-04 16:06:35

leetcode 154: Excel Sheet Column Title的相关文章

[LeetCode]62. 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

[LeetCode][JavaScript]Excel Sheet Column Title

https://leetcode.com/problems/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 我恨进制转换,傻傻算不清. /** * @par

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】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:经过分析不难发现实际上就是数对于 26 进制的转换用字母表示出来.代码如下: 1 class Solution: 2 # @param {in

leetCode(66)-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对应的该是第二个字母B,然而对应A,进位之后应该是BA,实际是AA,由此判断字母也是26进制,只是始终

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

【leetcode】Excel Sheet Column Title &amp; Excel Sheet Column Number (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 思路: 相当于10进制转26进制.与一般不一样的是10进制对应的是0 - 9.而这个26进制对应的是 A(1)- Z(26), 没有0. 我的代码: strin

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 Title python

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 python code class Solution: # @param {integer} n # @ret