Leet Code OJ 168. Excel Sheet Column Title [Difficulty: Easy]
Posted Lnho
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}
}
以上是关于Leet Code OJ 168. Excel Sheet Column Title [Difficulty: Easy]的主要内容,如果未能解决你的问题,请参考以下文章
Leet Code OJ 119. Pascal's Triangle II [Difficulty: Easy]
Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]
Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]
Leet Code OJ 1. Two Sum [Difficulty: Easy]