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
Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases.
Hide TagsMathHide Similar Problems(E) Excel Sheet Column numberAnalysis: Essentially a 10-to-26 conversion, but because the subscript starts at 1 instead of starting with 0, one operation is reduced. 1-"a26--" Z first take out the remainder, then convert it to character A~z, then n= (n-1)/26, the number of 26 to the right one bit, and then make a new judgment.
#include <stdio.h>#include<stdlib.h>#include<iostream>#include<vector>#include<queue>#include<stack>#include<algorithm>using namespacestd;voidPrintArray (int*array,intsize) { for(inti =0; i < size; i++) cout<< array[i]<<"\ t" ; cout<<Endl;}voidPrintvector (vector<int>Array) { for(inti =0; I <array.size (); i++) cout<< array[i]<<"\ t" ; cout<<Endl;}classSolution { Public: stringConverttotitle (intN) {stringstr; intA =0; while(n) {a= (n1) % -; STR+=Char(A +'A'); N= (n1)/ -; } reverse (Str.begin (), Str.end ()); returnstr; }};#if01-A2-B3-C ... --Z --AA --AB#endifintMain () {solution SL; cout<< Sl.converttotitle (1) <<Endl; cout<< Sl.converttotitle ( -) <<Endl; cout<< Sl.converttotitle ( +) <<Endl; cout<<Endl; return 0;}
[Leetcode] Excel Sheet Column Title