题目
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
压入栈
注意这是从1开始的
class Solution {
public static String convertToTitle(int n) {
Stack<Integer> stack = new Stack<>();
while (n>0) {
n--;
stack.push(n%26);
n = n/26;
}
StringBuffer ret = new StringBuffer("");
while (!stack.empty()){
System.out.println(stack.peek());
ret.append((char) (stack.pop()+65));
}
return ret.toString();
}
}