Total Accepted: 63607
Total Submissions: 286742
Difficulty: Easy
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
使用迭代的方法:
A-Z 0-25 0~25
AA-AZ 26-51 1*26+0~1*26+25
BA-BZ 52-78 2*26+0~2*26+25
AAA = AA + A = (1*26+0) + 0
BAA = BA + A = (2*26+0) + 0
public class Solution {
public String convertToTitle(int n) {
return n == 0 ? "" : convertToTitle((n-1)/26) + "" + (char)((n-1)%26 + 65);
}
}
可能表述的不太清楚。但是值得注意的是三位AAA这种情况,他可以通过迭代两位AA与最后一位A 的和来表示。