Skip to content

Commit 1a4de52

Browse files
add solution to excel sheet column number (#121)
1 parent 95c836c commit 1a4de52

File tree

2 files changed

+151
-112
lines changed

2 files changed

+151
-112
lines changed

C++/Excel-Sheet-Column-Number.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
//Problem Statement: Excel Sheet Column Number
3+
4+
// Given a column title as appear in an Excel sheet, return its corresponding column number.
5+
6+
// For example:
7+
8+
// A -> 1
9+
// B -> 2
10+
// C -> 3
11+
// ...
12+
// Z -> 26
13+
// AA -> 27
14+
// AB -> 28
15+
// ...
16+
17+
//Solution:
18+
class Solution {
19+
public:
20+
int titleToNumber(string s) {
21+
22+
int power=0, colnum=0;
23+
24+
while(!s.empty()) {
25+
26+
colnum += (s[s.size()-1] - 'A' + 1)*pow(26, power);
27+
power++;
28+
s=s.substr(0, s.size()-1);
29+
30+
}
31+
32+
return colnum;
33+
}
34+
};
35+
36+
//Complexity: O(n)

0 commit comments

Comments
 (0)