@
AntiGameZ #20 这东西要用矩阵去求取,不能用 26 进制。
public class ExcelSheetColumnTitle {
static Map<Integer, Character> index2titleMap = new HashMap<>();
static {
index2titleMap.put(1, 'A');
index2titleMap.put(2, 'B');
index2titleMap.put(3, 'C');
index2titleMap.put(4, 'D');
index2titleMap.put(5, 'E');
index2titleMap.put(6, 'F');
index2titleMap.put(7, 'G');
index2titleMap.put(8, 'H');
index2titleMap.put(9, 'I');
index2titleMap.put(10, 'J');
index2titleMap.put(11, 'K');
index2titleMap.put(12, 'L');
index2titleMap.put(13, 'M');
index2titleMap.put(14, 'N');
index2titleMap.put(15, 'O');
index2titleMap.put(16, 'P');
index2titleMap.put(17, 'Q');
index2titleMap.put(18, 'R');
index2titleMap.put(19, 'S');
index2titleMap.put(20, 'T');
index2titleMap.put(21, 'U');
index2titleMap.put(22, 'V');
index2titleMap.put(23, 'W');
index2titleMap.put(24, 'X');
index2titleMap.put(25, 'Y');
index2titleMap.put(26, 'Z');
}
public String valueToTitle(int n) {
int row = n / 26;
int column = n % 26;
if (column == 0) {
row -= 1;
column = 26;
}
if (row == 0) {
return String.valueOf(index2titleMap.get(column));
} else if (row >= 1 && row <= 26) {
return new StringBuilder().append(index2titleMap.get(row)).append(index2titleMap.get(column)).toString();
} else {
return null;
}
}
}