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
class Solution {
public String convertToTitle(int n) {
String str = new String("ZABCDEFGHIJKLMNOPQRSTUVWXY");
StringBuilder res = new StringBuilder();
while(n>0){
res.insert(0,String.valueOf(str.charAt(n%26)));
if(n %26 == 0) n--;
n /= 26;
}
return res.toString();
}
}
微信扫一扫
支付宝扫一扫