Skip to content

Latest commit

 

History

History
24 lines (18 loc) · 507 Bytes

File metadata and controls

24 lines (18 loc) · 507 Bytes

Excel Sheet Column Title

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

Solution

进制转化,其实质就是把数字转成26进制

由于1->A, 2->B, 26->Z,即所以需要把n-1在操作

Code

string convertToTitle(int n) {
	string result;
	while (n) {
		result.push_back(--n % 26 + 'A');
		n /= 26;
	}
	return string(result.rbegin(), result.rend());
}

扩展

逆过程Excel Sheet Column Number