Skip to content

Commit 798c7cc

Browse files
committed
feat: add solution of Excel Sheet Column Title(168) with javascript.
1 parent 1cf3c42 commit 798c7cc

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
| [155][155-question] | [Min Stack][155-tips] | [Easy][E] | [][155-java] | [][155-js] | [][155-kotlin] |
9292
| [160][160-question] | [Intersection of Two Linked Lists][160-tips] | [Easy][E] | [][160-java] | | - |
9393
| [167][167-question] | [Two Sum II - Input array is sorted][167-tips] | [Easy][E] | | [][167-js] | - |
94+
| [168][168-question] | [Excel Sheet Column Title][168-tips] | [Easy][E] | | [][168-js] | - |
9495
| [226][226-question] | [Invert Binary Tree][226-tips] | [Easy][E] | [][226-java] | [][226-js] | [][226-kotlin] |
9596
| [504][504-question] | [Base 7][504-tips] | [Easy][E] | | | [][504-kotlin] |
9697
| [543][543-question] | [Diameter of Binary Tree][543-tips] | [Easy][E] | [][543-java] | | [][543-kotlin] |
@@ -290,6 +291,7 @@ commit信息模板: ``feat: add the solution of `Two Sum`(001) with Java``
290291
[155-question]: https://leetcode.com/problems/min-stack/
291292
[160-question]: https://leetcode.com/problems/intersection-of-two-linked-lists/
292293
[167-question]: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
294+
[168-question]: https://leetcode.com/problems/excel-sheet-column-title/
293295
[226-question]: https://leetcode.com/problems/invert-binary-tree/
294296
[504-question]: https://leetcode.com/problems/base-7/description/
295297
[543-question]: https://leetcode.com/problems/diameter-of-binary-tree/
@@ -396,6 +398,7 @@ commit信息模板: ``feat: add the solution of `Two Sum`(001) with Java``
396398
[155-tips]: ./tips/155/README.md
397399
[160-tips]: ./tips/160/README.md
398400
[167-tips]: ./tips/167/README.md
401+
[168-tips]: ./tips/168/README.md
399402
[226-tips]: ./tips/226/README.md
400403
[504-tips]: ./tips/504/README.md
401404
[543-tips]: ./tips/543/README.md
@@ -472,6 +475,7 @@ commit信息模板: ``feat: add the solution of `Two Sum`(001) with Java``
472475
[136-js]: ./src/_136/Solution.js
473476
[155-js]: ./src/_155/Solution.js
474477
[167-js]: ./src/_167/Solution.js
478+
[168-js]: ./src/_168/Solution.js
475479
[226-js]: ./src/_226/Solution.js
476480
[561-js]: ./src/_561/Solution.js
477481
[643-js]: ./src/_643/Solution.js

src/_168/Solution.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number} n
3+
* @return {string}
4+
*/
5+
var convertToTitle = function(n) {
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 lastLetter = n % 26 === 0 ? 'Z' : letter[n % 26 - 1]
8+
if(n <= 26) {
9+
return lastLetter
10+
}
11+
let nextLetterIndex = (n - (n % 26 === 0 ? 26 : n % 26))/26
12+
if(Math.floor(nextLetterIndex) <= 26) {
13+
return letter[Math.floor(nextLetterIndex) - 1] + lastLetter
14+
} else {
15+
return convertToTitle(Math.floor(n/26)) + lastLetter
16+
}
17+
};

tips/167/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
2424
## 思路 1
2525

2626
javascript:
27-
```java
27+
```javascript
2828
/**
2929
* @param {number[]} numbers
3030
* @param {number} target

tips/168/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
[Excel Sheet Column Title][title]
2+
3+
## Description
4+
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
5+
6+
7+
**Example:**
8+
9+
```
10+
1 -> A
11+
2 -> B
12+
3 -> C
13+
...
14+
26 -> Z
15+
27 -> AA
16+
28 -> AB
17+
...
18+
19+
20+
Input: 1
21+
Output: "A"
22+
23+
Input: 28
24+
Output: "AB"
25+
26+
Input: 701
27+
Output: "ZY"
28+
```
29+
30+
**Tags:**
31+
`Array`
32+
33+
## 思路 1
34+
35+
javascript:
36+
```javascript
37+
/**
38+
* @param {number} n
39+
* @return {string}
40+
*/
41+
var convertToTitle = function(n) {
42+
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']
43+
let lastLetter = n % 26 === 0 ? 'Z' : letter[n % 26 - 1]
44+
if(n <= 26) {
45+
return lastLetter
46+
}
47+
let nextLetterIndex = (n - (n % 26 === 0 ? 26 : n % 26))/26
48+
if(Math.floor(nextLetterIndex) <= 26) {
49+
return letter[Math.floor(nextLetterIndex) - 1] + lastLetter
50+
} else {
51+
return convertToTitle(Math.floor(n/26)) + lastLetter
52+
}
53+
};
54+
```
55+
56+
## 结语
57+
58+
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
59+
60+
[title]: https://leetcode.com/problems/excel-sheet-column-title/
61+
[ls]: https://github.com/RichCodersAndMe/LeetCode-Solution

0 commit comments

Comments
 (0)