File tree 1 file changed +76
-0
lines changed
1 file changed +76
-0
lines changed Original file line number Diff line number Diff line change
1
+ # 171. Excel Sheet Column Number
2
+
3
+ - Difficulty: Easy.
4
+ - Related Topics: Math.
5
+ - Similar Questions: Excel Sheet Column Title.
6
+
7
+ ## Problem
8
+
9
+ Given a column title as appear in an Excel sheet, return its corresponding column number.
10
+
11
+ For example:
12
+
13
+ ```
14
+ A -> 1
15
+ B -> 2
16
+ C -> 3
17
+ ...
18
+ Z -> 26
19
+ AA -> 27
20
+ AB -> 28
21
+ ...
22
+ ```
23
+
24
+ ** Example 1:**
25
+
26
+ ```
27
+ Input: "A"
28
+ Output: 1
29
+ ```
30
+
31
+ ** Example 2:**
32
+
33
+ ```
34
+ Input: "AB"
35
+ Output: 28
36
+ ```
37
+
38
+ ** Example 3:**
39
+
40
+ ```
41
+ Input: "ZY"
42
+ Output: 701
43
+ ```
44
+
45
+ ## Solution
46
+
47
+ ``` javascript
48
+ /**
49
+ * @param {string} s
50
+ * @return {number}
51
+ */
52
+ var titleToNumber = function (s ) {
53
+ var res = 0 ;
54
+ var num = 0 ;
55
+ var len = s .length ;
56
+ for (var i = 0 ; i < len; i++ ) {
57
+ num = getNum (s[len - 1 - i]);
58
+ res += Math .pow (26 , i) * num;
59
+ }
60
+ return res;
61
+ };
62
+
63
+ var getNum = function (char ) {
64
+ var start = ' A' .charCodeAt (0 ) - 1 ;
65
+ return char .charCodeAt (0 ) - start;
66
+ };
67
+ ```
68
+
69
+ ** Explain:**
70
+
71
+ nope.
72
+
73
+ ** Complexity:**
74
+
75
+ * Time complexity : O(n).
76
+ * Space complexity : O(1).
You can’t perform that action at this time.
0 commit comments