"Leetcode" Excel Sheet column Title & Excel Sheet column number (easy)

Source: Internet
Author: User

Given a positive integer, return its corresponding column title as appear in an Excel sheet.

For example:

    1, A    2, B    3, C    ...    AA, Z        

Ideas:

Equivalent to 10 binary to 26 binary. Unlike the general one, the 10 binary corresponds to 0-9. And this 26 binary corresponds to a (1)-Z (26), not 0.

My Code:

stringConverttotitle (intN) {stringans;  while(n! =0)        {            intnum = n -; N/= -; if(num! =0) {ans.push_back (num-1+'A'); }            Else{ans.push_back ('Z'); N--;        }} reverse (Ans.begin (), Ans.end ()); returnans; }

The Great God streamlined code:

 string  converttotitle (int   N) { string   res;     char   TMP;  while   (n) {n -= 1   =  a   '  + n% 26          = tmp + res;    n /= 26   return   res;}  

Given a column title as appear in an Excel sheet, and return its corresponding column number.

For example:

    1,    B, 2    C-3    ...    27 AA, Z    

Idea: 26 binary to 10 binary. Take AAA as an example AAA = A * 262 + A * 261 + A * 260;

 int  titletonumber (string   s) { int  ans = 0  ;  int  factor = 1  ;  while  (! = ans + (s.back ()-  a   " + 1 ) *             factor;            S.pop_back ();        Factor  *= ;     return   ans; }

The Great God Lite version:

int 0 ;  for (int0'A'1), i++); return result;

"Leetcode" Excel Sheet column Title & Excel Sheet column number (easy)

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.