#168
题目:
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
题解:
其实就是把十进制转换为26进制。
public class Solution {
public String convertToTitle(int n) {
String res = "";
while(n != 0){
char c = (char)((n-1)%26+'A');
res = c+res;
n = (n-1)/26;
}
return res;
}
}
#171
题目:
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
题解:
这个与上面相反。是把26进制转换为十进制。
public class Solution {
public int titleToNumber(String s) {
int ret=0;
for(int i=0;i<s.length();i++){
ret=ret*26+s.charAt(i)-'A'+1;//26进制哦,另外注意+1
}
return ret;
}
}