Topic:
Converts a number to a character representation in the form of Excel. Example: 1->a 2->b 3->c ... 26->z 27->aa ...
Problem Solving Ideas:
At first glance it's a bit like a conversion problem, but think about it without ' 0 '. So this time we need to observe carefully: it is obvious that this is a 26 notation, but there is no 0 representation method. So let's sum up, the decimal 10 in here, how to say, is z,20 it? B*,30 is c*, and so on:
10-z20-b*30-c* ... 260-yz
That is, when n>0 and n%26 = = 0 o'clock, we output z, and up a borrow 1 (here a bit like division of borrow), in other words, when n%26==0, we output z, in the subsequent calculation of n should be equal to (n-1)/26. So the code is as follows
Class solution (Object): def converttotitle (self, N): "" " : Type n:int : Rtype:str " "" ret = " alpha = ' zabcdefghijklmnopqrstuvwxy ' while n > 0: ret = alpha[n%26] + ret n = (n-1)/+ return Ret
Leetcode-excel Sheet Column Title