Skip to content

Commit 23a112e

Browse files
committed
Add Excel Sheet Column Number
1 parent 214510c commit 23a112e

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ Princeton Algorithms [Part 1](https://www.coursera.org/learn/algorithms-part1/)
196196
* Recursive [C++](problems/math/PascalTriangle.cpp)
197197
* Iterative [C++](problems/math/PascalTriangle.cpp)
198198
* Happy number [Wiki](https://en.wikipedia.org/wiki/Happy_number) [C++](problems/math/HappyNumber.cpp)
199+
* Excel Sheet Column Number [C++](problems/math/SheetColumnNumber.cpp)
199200

200201
## Permutation
201202
* Permutation of objects [Java](problems/queue/Permutation.java)

problems/math/SheetColumnNumber.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Leetcode https://leetcode.com/explore/interview/card/top-interview-questions-medium/113/math/817/
2+
3+
#include <iostream>
4+
#include <string>
5+
#include <vector>
6+
7+
const int BASE = 26;
8+
9+
int titleToNumber(const std::string& columnTitle)
10+
{
11+
int number = 0;
12+
for (auto ch : columnTitle) {
13+
number = number * BASE + (ch - 'A' + 1);
14+
}
15+
return number;
16+
}
17+
18+
void test(const std::string& columnTitle)
19+
{
20+
std::cout << "Column title : " << columnTitle << " , number : " << titleToNumber(columnTitle) << "\n";
21+
std::cout << "=====================================\n";
22+
}
23+
24+
int main()
25+
{
26+
test("A");
27+
test("AB");
28+
test("ZY");
29+
test("FXSHRXW");
30+
31+
return 0;
32+
}

0 commit comments

Comments
 (0)