Skip to content

Commit 8caa089

Browse files
committed
feat: add solution of Excel Sheet Column Number(171) with javascript.
1 parent 1ddf3a4 commit 8caa089

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
| [167][167-question] | [Two Sum II - Input array is sorted][167-tips] | [Easy][E] | | [][167-js] | - |
9494
| [168][168-question] | [Excel Sheet Column Title][168-tips] | [Easy][E] | | [][168-js] | - |
9595
| [169][169-question] | [Majority Element][169-tips] | [Easy][E] | | [][169-js] | - |
96+
| [171][171-question] | [excel-sheet-column-number][171-tips] | [Easy][E] | | [][171-js] | - |
9697
| [226][226-question] | [Invert Binary Tree][226-tips] | [Easy][E] | [][226-java] | [][226-js] | [][226-kotlin] |
9798
| [504][504-question] | [Base 7][504-tips] | [Easy][E] | | | [][504-kotlin] |
9899
| [543][543-question] | [Diameter of Binary Tree][543-tips] | [Easy][E] | [][543-java] | | [][543-kotlin] |
@@ -294,6 +295,7 @@ commit信息模板: ``feat: add the solution of `Two Sum`(001) with Java``
294295
[167-question]: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
295296
[168-question]: https://leetcode.com/problems/excel-sheet-column-title/
296297
[169-question]: https://leetcode.com/problems/majority-element/
298+
[171-question]: https://leetcode.com/problems/excel-sheet-column-number/
297299
[226-question]: https://leetcode.com/problems/invert-binary-tree/
298300
[504-question]: https://leetcode.com/problems/base-7/description/
299301
[543-question]: https://leetcode.com/problems/diameter-of-binary-tree/
@@ -402,6 +404,7 @@ commit信息模板: ``feat: add the solution of `Two Sum`(001) with Java``
402404
[167-tips]: ./tips/167/README.md
403405
[168-tips]: ./tips/168/README.md
404406
[169-tips]: ./tips/169/README.md
407+
[171-tips]: ./tips/171/README.md
405408
[226-tips]: ./tips/226/README.md
406409
[504-tips]: ./tips/504/README.md
407410
[543-tips]: ./tips/543/README.md
@@ -480,6 +483,7 @@ commit信息模板: ``feat: add the solution of `Two Sum`(001) with Java``
480483
[167-js]: ./src/_167/Solution.js
481484
[168-js]: ./src/_168/Solution.js
482485
[169-js]: ./src/_169/Solution.js
486+
[171-js]: ./src/_171/Solution.js
483487
[226-js]: ./src/_226/Solution.js
484488
[561-js]: ./src/_561/Solution.js
485489
[643-js]: ./src/_643/Solution.js

src/_171/Solution.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var titleToNumber = function(s) {
6+
let letter = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
7+
let letterArr = s.split('').reverse()
8+
let number = 0
9+
for(let i = 0; i < letterArr.length; i++) {
10+
let index = letter.findIndex(item => item === letterArr[i]) + 1
11+
number += i === 0 ? index : index*Math.pow(26,i)
12+
}
13+
return number
14+
};

tips/171/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
[Excel Sheet Column Number][title]
2+
3+
## Description
4+
Given a column title as appear in an Excel sheet, return its corresponding column number.
5+
6+
**Example:**
7+
8+
```
9+
A -> 1
10+
B -> 2
11+
C -> 3
12+
...
13+
Z -> 26
14+
AA -> 27
15+
AB -> 28
16+
...
17+
18+
19+
Input: "A"
20+
Output: 1
21+
22+
Input: "AB"
23+
Output: 28
24+
25+
Input: "ZY"
26+
Output: 701
27+
```
28+
29+
**Tags:**
30+
`Array`
31+
32+
## 思路 1
33+
34+
javascript:
35+
```javascript
36+
/**
37+
* @param {string} s
38+
* @return {number}
39+
*/
40+
var titleToNumber = function(s) {
41+
let letter = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
42+
let letterArr = s.split('').reverse()
43+
let number = 0
44+
for(let i = 0; i < letterArr.length; i++) {
45+
let index = letter.findIndex(item => item === letterArr[i]) + 1
46+
number += i === 0 ? index : index*Math.pow(26,i)
47+
}
48+
return number
49+
};
50+
```
51+
52+
## 结语
53+
54+
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
55+
56+
[title]: https://leetcode.com/problems/excel-sheet-column-number/
57+
[ls]: https://github.com/RichCodersAndMe/LeetCode-Solution

0 commit comments

Comments
 (0)