Skip to content

Commit 667dfde

Browse files
committed
Solve problem '171) excel sheet column number'
1 parent 8032213 commit 667dfde

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Given a string **columnTitle** that represents the column title as appear in an Excel sheet, return _its corresponding column number_.
2+
3+
For example:
4+
5+
```
6+
A -> 1
7+
B -> 2
8+
C -> 3
9+
...
10+
Z -> 26
11+
AA -> 27
12+
AB -> 28
13+
...
14+
```
15+
16+
**Example 1:**
17+
18+
```
19+
Input: columnTitle = "A"
20+
Output: 1
21+
```
22+
23+
**Example 2:**
24+
25+
```
26+
Input: columnTitle = "AB"
27+
Output: 28
28+
```
29+
30+
**Example 3:**
31+
32+
```
33+
Input: columnTitle = "ZY"
34+
Output: 701
35+
```
36+
37+
**Constraints:**
38+
39+
- `1 <= columnTitle.length <= 7`
40+
- `columnTitle` consists only of uppercase English letters.
41+
- `columnTitle` is in the range `["A", "FXSHRXW"]`.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
fmt.Println(titleToNumber("A"))
7+
fmt.Println(titleToNumber("AB"))
8+
fmt.Println(titleToNumber("ZY"))
9+
}
10+
11+
func titleToNumber(columnTitle string) int {
12+
result, val := 0, 0
13+
for i := 0; i < len(columnTitle); i++ {
14+
val = int(columnTitle[i] - 'A' + 1)
15+
result = result*26 + val
16+
}
17+
18+
return result
19+
}

0 commit comments

Comments
 (0)