File tree Expand file tree Collapse file tree 2 files changed +60
-0
lines changed
problems/171-excel-sheet-column-number Expand file tree Collapse file tree 2 files changed +60
-0
lines changed Original file line number Diff line number Diff line change 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"] ` .
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments