-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexcel_sheet_column_title.cpp
48 lines (44 loc) · 1.19 KB
/
excel_sheet_column_title.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// =====================================================================================
//
// Filename: excel_sheet_column_title.cpp
//
// Description: 168. Excel Sheet Column Title. Given a positive integer, return its
// corresponding column title as appear in an Excel sheet.
//
// Version: 1.0
// Created: 09/16/2019 07:14:26 PM
// Revision: none
// Compiler: g++
//
// Author: Zhu Xianfeng (), [email protected]
// Organization:
//
// =====================================================================================
#include <stdio.h>
#include <stdlib.h>
#include <string>
class Solution
{
public:
std::string convertToTitle(int n)
{
const int kLetterCnt = 'Z' - 'A' + 1;
if (n <= kLetterCnt)
{
return std::string() + (char)(n + 'A' - 1);
}
n--;
return convertToTitle(n / kLetterCnt) + convertToTitle(n % kLetterCnt + 1);
}
};
int main(int argc, char* argv[])
{
int num = 701;
if (argc > 1)
{
num = atoi(argv[1]);
}
std::string title = Solution().convertToTitle(num);
printf("%d -> %s\n", num, title.c_str());
return 0;
}