Excel Sheet Column Title
Original title link Excel Sheet Column Title
The number to the letter ' A ' to ' Z ' indicates that the corresponding relationship is shown in figure
Ideas:
Essentially, the 10 is converted to 26, and the results are different from [0:25] [0:25] to [1:26] [1:26]
Assuming 1<=n%26<=26 1, then 0<= (n−1)%26<=25 0, so you can subtract one at a time from each modulo
The code is as follows
Class Solution {public
:
string converttotitle (int n) {
string res ("");
Do
{
res.append (1, ' A ' + (n-1)%);
/* N-1 is designed to solve the case where the modulo result is 0 *
/n = (n-1)/+;
} while (n);
Std::reverse (Res.begin (), Res.end ());
return res;
}
};
Excel Sheet Column number
Original title link Excel Sheet Column number
The reverse of the previous question, directly into the 26 binary can be
The code is as follows
class Solution {Public:int Titletonumber (string s) {return std::accumulate (s.begin
(), S.end (), 0, [] (auto N, auto value) {
return n * + (value-' A ' + 1);
}
); }
};