Title Address:
https://oj.leetcode.com/problems/excel-sheet-column-title/
Topic content:
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:
1, A 2, B 3, C ... AB, AA, Z
Ideas:
At first glance is very troublesome, may wish to start from the reverse: Know the letter combination, how to be realistic.
This is relatively simple.
A B
1 * 26 + 2 = 28
A A Z
1 * 26 * 26 + 1 * 26 + 26 = 728
A bit like 27 binary, but there is a decisive difference: There is no number 0 here. Therefore, the direct analogy is 27 binary, there is no 0 for reference, if the analogy is 26, actually 26 is not in, or Z.
In fact, we can refer to the algorithm of the digital conversion, each round to find the lowest bit. the key to the problem is how to find the lowest bit.
The first modulo 26, if 0 is Z, the rest and a to Y one by one corresponds to, that is, a=1,b=2...y=25.
Second, "to subtract the lowest bit and then do the division." Because this is not a real 26 binary, so if the lowest bit is Z, the Direct division does not completely delete Z, leaving a 1. therefore, when the lowest bit is Z, we subtract 26 and then divide to eliminate this low-level weight.
All code:
classSolution { Public: stringConverttotitle (intN) {Charres[101]; Char*dic ="Zabcdefghijklmnopqrstuvwxy"; intStart = -; stringtar; while(N >0) { if(n% -==0) {res[--start] ='Z'; N-= -; } Else{res[--start] = dic[n% -]; N-= n% -; } N/= -; } res[ -] =' /'; Tar= Res +start; returntar; }};
"Original" Leetcodeoj---Excel Sheet Column Title Problem Solving report