[LeetCode] Excel Sheet Column Title
2014-12-28 01:26
211 查看
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:
Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases.
public class Solution {
public String convertToTitle(int n) {
ArrayList<Character> res = new ArrayList<Character>();
while(n > 26) {
int tmp = n % 26;
if (tmp == 0) res.add(0, 'Z');
else res.add(0, (char) ('A' + tmp-1));
int m = n;
n /= 26;
if ( n*26 - m == 0) n -= 1;
}
res.add(0,(char)('A' + n-1));
String ress = new String();
for (int i = 0; i < res.size(); i++) {
ress += res.get(i);
}
return ress;
}
}
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) {
ArrayList<Character> res = new ArrayList<Character>();
while(n > 26) {
int tmp = n % 26;
if (tmp == 0) res.add(0, 'Z');
else res.add(0, (char) ('A' + tmp-1));
int m = n;
n /= 26;
if ( n*26 - m == 0) n -= 1;
}
res.add(0,(char)('A' + n-1));
String ress = new String();
for (int i = 0; i < res.size(); i++) {
ress += res.get(i);
}
return ress;
}
}
相关文章推荐
- LeetCode - 168/171 - Excel Sheet Column Title/Number
- LeetCode Excel Sheet Column Title
- [LeetCode]--168. Excel Sheet Column Title
- 【Leetcode】Excel sheet Column title
- 【leetcode】 Excel_Sheet_Column_Title
- Leetcode 168 Excel Sheet Column Title
- Leetcode33: Excel Sheet Column Title
- leetcode笔记--Excel Sheet Column Title
- [LeetCode] Excel Sheet Column Title 求Excel表列名称
- LeetCode-168 Excel Sheet Column Title
- [LeetCode]Excel Sheet Column Title
- leetcode: Excel Sheet Column Title
- 【LeetCode-面试算法经典-Java实现】【168-Excel Sheet Column Title(Excell列标题)】
- leetcode(168)Excel Sheet Column Title 简单题
- LeetCode之Excel Sheet Column Title
- LeetCode题解:Excel Sheet Column Title
- [leetcode] Excel Sheet Column Title
- 【LeetCode】Excel Sheet Column Title
- [Leetcode]168. Excel Sheet Column Title
- LeetCode --Excel Sheet Column Title