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 ...
Example 1:
Input: 1 Output: "A"
Example 2:
Input: 28 Output: "AB"
Example 3:
Input: 701 Output: "ZY"
class Solution { public String convertToTitle(int n) { List<Character> list = new ArrayList(); Stack<Character> stack = new Stack(); for(char c = 'A'; c <= 'Z'; c++) list.add(c); while(n != 0){ n--; char k = list.get(n % 26 ); //System.out.println(k); stack.push(k); n = n / 26; } StringBuilder sb = new StringBuilder(); while(!stack.isEmpty()) sb.append(stack.pop()); return sb.toString(); } }
有病??
注意因为是0-index的list,所以n进去要先减一
class Solution { public String convertToTitle(int n) { String ret = ""; while (n != 0) { ret = (char)('A'+(--n)%26) + ret; n /= 26; } return ret; } }