fengsehng

# 题目：

``````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进制，只是始终要小一位。因此在换算时候需要（n-1）一直循环。

# 代码：

``````public class Solution {
public String convertToTitle(int n) {
StringBuffer sb = new StringBuffer();
if(n < 1){
return null;
}
while((n-1)/26 > 0){
char i =(char)((n-1)%26+'A');
sb.append(i);
n = (n-1)/26;
}
char a = (char)((n-1) % 26+'A');
sb.append(a);
return sb.reverse().toString();
}
}``````

### 评论(0)

Excel Sheet Column Number(leetcode171)

Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 Example 1: Input: "A"Output: 1 Example 2: Input: "AB"Output: 28 Exa......

woshixin
2018/11/12
15
0
leetcode math类型题目解题总结

Add Two Numbers https://leetcode.com/problems/add-two-numbers/description/ class Solution {public: };View Code 7. Reverse Integer https://leetcode.com/problems/reverse-integer/d......

osc_xbmy69c2
2019/03/12
2
0
LeetCode - Easy - Excel Sheet Column Number

Given a column title as appear in an Excel sheet, return its corresponding column number. For example: thinking: 进制之间的装换 solution: public class ExcelSheetColumnNumber {p......

2018/03/28
39
0
leetcode 171: 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 ->......

2015/02/03
841
0
LeetCode-171.Excel Sheet Colunm Number

Given a column title as appear in an Excel sheet, return its corresponding column number. For example: Example 1: Example 2: Example 3: 一个找规律的数学题,找找规律就出来了 solu......

Sunlight_blow
2018/10/24
0
0

40分钟前
34
0

1.添加依赖： yum install -y yum-utils device-mapper-persistent-data lvm2 2、添加软件源 yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.r......

muoushi
53分钟前
27
0

54分钟前
35
0

58分钟前
44
0
spring boot 设置任务调度线程池大小和线程前缀

jingshishengxu

26
0