p168

LeetCode 168 Excel Sheet Column Title 题解

1.题目:

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进制

2.解题思路:

26进制

3.代码


[title] [] [url] [link text]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 
package leetcode;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Scanner;
import java.util.SortedMap;

public class p168 {
public static void main(String[] args) {
String ans = null;
ans = convertToTitle(27);
System.out.println(ans);

}

public static String convertToTitle(int n) {

StringBuffer a = new StringBuffer();
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = n; i > 0;) {

int p = (i - 1) % 26;
list.add(p);
//System.out.println(p);
i = (i - 1) / 26;

}
for (int i = list.size() - 1; i >= 0; i--) {

a.append((char) (list.get(i) + 'A'));

}
String ans = a.toString();
return ans;
}

}



4.一些总结: