Leetcode-excel Sheet Column Title

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.