Repository navigation
Expand file tree
/
Copy pathExcelSheetColNo.cpp
More file actions
83 lines (66 loc) · 1.99 KB
/
Copy pathExcelSheetColNo.cpp
File metadata and controls
83 lines (66 loc) · 1.99 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
Given a string columnTitle that represents the column title as appear in an Excel sheet, return its corresponding column number.
For example:
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
Example 1:
Input: columnTitle = "A"
Output: 1
Example 2:
Input: columnTitle = "AB"
Output: 28
Example 3:
Input: columnTitle = "ZY"
Output: 701
*/
#include <bits/stdc++.h>
using namespace std;
int titleToNumber(string colNumber)
{
int res=0;
for(auto it:colNumber)
{
int t=it-'A'+1;
res=res*26+t;//BCA B=2*26*26 A=1*26 A=1 ==>89
}
return res;
}
int main()
{
// cout<<"Hello World";
cout<<titleToNumber("BAA");
return 0;
}
/*
------------------Explanation------------------
1. There are 26 letters in our alphabet and we start counting from 1, not zero.
So 'Z' is 26.
2. The rest of the combinations start from a base 26
AA --> 26*1+ 1 = 27 (A == 1)
AB --> 26*1+ 2 = 28 (B == 2)
AC -->26*1 + 3 = 29 (C == 3)
.....
So we can write like this:
result = 0
d = s[i](char) - 'A' + 1 (we used s[i] - 'A' to convert the letter to a number like it's going to be C)
result = result* 26 + d
If the given input is only one letter, it will automatically take the value s[i] - 'A' + 1 as the first result is 0.
Some More Explanation
1. For every additional digit of the string, we multiply the value of the digit by 26^n
2. here n is the number of digits it is away from the one's place.
3. This is similar to how the number 254 could be broken down as this:
(2 x 10 x 10) + (5 x 10) + (4).
4. The reason we use 26 instead of 10 is because 26 is our base.
For s = "BCM" the final solution would be (2 x 26 x 26) + (3 x 26) + (13)
We could do this process iteratively. Start at looking at the first digit "B". Add the int equivalent of "B" to the running sum and continue.
Every time we look at the following digit multiply our running sum by 26 before adding the next digit to signify we are changing places. Example below:
"B" = 2
"BC" = (2)26 + 3
"BCM" = (2(26) + 3)26 + 13
*/