168 . Excel Sheet Column Title
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
3ms:
public String convertToTitle(int n) {
StringBuffer x = new StringBuffer();
while(n>0){
x.append((char)((n-1)%26+'A'));
n = (n-1)/26;
}
return x.reverse().toString();
}
171 . Excel Sheet Column Number
Easy
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
3ms:
public int titleToNumber(String s) {
int len = s.length();
int value = 0;
for(int i=0;i<len;i++){
char x = s.charAt(i);
value = value*26+(x-'A')+1;
}
return value;
}
本文介绍了如何在Excel中将列号转换为对应的列标题,以及如何将列标题转换回对应的列号。提供了两个Java方法实现:convertToTitle用于从列号到列标题的转换;titleToNumber用于从列标题到列号的转换。
490

被折叠的 条评论
为什么被折叠?



