"Original" Leetcodeoj---Excel Sheet Column Title Problem Solving report

Source: Internet
Author: User

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

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.