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)