Leetcode#168Excel 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

public class Solution {

public String convertToTitle(int n) {

String c="";

while(n>0)

{

int x=n%26;

if(x==0)

{

c=String.valueOf((char)(‘A‘-1+26))+c;

n=n/26-1;

}

else

{

c=String.valueOf((char)(‘A‘-1+x))+c;

n=n/26;

}

}

return c;

}

}

时间: 2024-10-10 23:01:40

Leetcode#168Excel Sheet Column Title的相关文章

【LeetCode-面试算法经典-Java实现】【168-Excel Sheet Column Title(Excell列标题)】

[168-Excel Sheet Column Title(Excell列标题)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 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

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

[leetcode] Excel Sheet Column Title & Excel Sheet Column Number

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 思路: 10进制转26进制 .先求低位再求高位,与10进制转2进制一样. 题解: class Solution

[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

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 这里就是一个将数字转化为字母的部分,其实就是一个将10进制转化为26进制 #include <string> using namespace std

LeetCode - Excel Sheet Column Title

时隔几分钟又来写一个题,这个应该算个水题. public class Solution { public String convertToTitle(int n) { StringBuilder ans = new StringBuilder(); if(n < 1) ans.append(""); else { while(n-- > 0) { char ch = (char) (n % 26 + 'A'); ans.append(ch); n /= 26; } } re

LeetCode Excel Sheet Column Title (输出excel表的列名称)

题意:给一个数字n,输出excel表的列名称. 思路:其实观察可知道,是个26进制的标记而已.那就模拟一下,每次计算一位时就先左移1位,再进行计算. 1 class Solution { 2 public: 3 string convertToTitle(int n) { 4 string ans=""; 5 while(n) //26进制,但是输出时不同而已,是从1开始,而不是0.每次计算将其先左移1位即可. 6 { 7 ans+=(n-1)%26+'A'; 8 n=(n-1-(n-

2016.5.19——Excel Sheet Column Title

Excel Sheet Column Title 本题收获: 1.由int型转换为整型(string),如何转化, res = 'A'+(n-1)%26和之前由A-z转化为十进制相反,res = s[i]-'A'+1.(为什么有+1,-1还有点迷糊,貌似是十进制是0-9,26进制是) 2.十进制到26进制转化 题目: Given a positive integer, return its corresponding column title as appear in an Excel shee

[LeetCode] Excel Sheet Column Number

Excel Sheet Column Number 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 解题思路: 看成是进制转