diff --git a/README.md b/README.md index 4f640d3..4cac319 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@
- + diff --git a/assets/data/categories.json b/assets/data/categories.json index e6bf4ae..acba2b7 100644 --- a/assets/data/categories.json +++ b/assets/data/categories.json @@ -407,6 +407,12 @@ "path": "./problemset/compare-version-numbers/README.md", "difficulty": "中等" }, + { + "id": "171", + "title": "Excel 表列序号", + "path": "./problemset/excel-sheet-column-number/README.md", + "difficulty": "简单" + }, { "id": "541", "title": "反转字符串 II", @@ -738,6 +744,12 @@ "path": "./problemset/single-number/README.md", "difficulty": "简单" }, + { + "id": "171", + "title": "Excel 表列序号", + "path": "./problemset/excel-sheet-column-number/README.md", + "difficulty": "简单" + }, { "id": "292", "title": "Nim 游戏", @@ -1632,6 +1644,12 @@ "path": "./problemset/subsets-ii/README.md", "difficulty": "中等" }, + { + "id": "171", + "title": "Excel 表列序号", + "path": "./problemset/excel-sheet-column-number/README.md", + "difficulty": "简单" + }, { "id": "187", "title": "重复的DNA序列", diff --git a/assets/data/topics.json b/assets/data/topics.json index a59ac33..0f0e9c3 100644 --- a/assets/data/topics.json +++ b/assets/data/topics.json @@ -859,6 +859,16 @@ "url": "https://leetcode.cn/problems/majority-element/", "path": "./problemset/majority-element/README.md" }, + { + "id": "171", + "title": { + "cn": "Excel 表列序号", + "en": "excel-sheet-column-number" + }, + "difficulty": "简单", + "url": "https://leetcode.cn/problems/excel-sheet-column-number/description/", + "path": "./problemset/excel-sheet-column-number/README.md" + }, { "id": "187", "title": { diff --git a/assets/docs/CATEGORIES.md b/assets/docs/CATEGORIES.md index d414892..4d65820 100644 --- a/assets/docs/CATEGORIES.md +++ b/assets/docs/CATEGORIES.md @@ -84,6 +84,7 @@ | 58. [最后一个单词的长度](../../problemset/length-of-last-word/README.md) | 简单 | | 151. [反转字符串中的单词](../../problemset/reverse-words-in-a-string/README.md) | 中等 | | 165. [比较版本号](../../problemset/compare-version-numbers/README.md) | 中等 | +| 171. [Excel 表列序号](../../problemset/excel-sheet-column-number/README.md) | 简单 | | 541. [反转字符串 II](../../problemset/reverse-string-ii/README.md) | 简单 | | 557. [反转字符串中的单词 III](../../problemset/reverse-words-in-a-string-iii/README.md) | 简单 | | 1016. [子串能表示从 1 到 N 数字的二进制串](../../problemset/binary-string-with-substrings-representing-1-to-n/README.md) | 中等 | @@ -160,6 +161,7 @@ | 118. [杨辉三角](../../problemset/pascals-triangle/README.md) | 简单 | | 119. [杨辉三角 II](../../problemset/pascals-triangle-ii/README.md) | 简单 | | 136. [只出现一次的数字](../../problemset/single-number/README.md) | 简单 | +| 171. [Excel 表列序号](../../problemset/excel-sheet-column-number/README.md) | 简单 | | 292. [Nim 游戏](../../problemset/nim-game/README.md) | 简单 | | 326. [3 的幂](../../problemset/power-of-three/README.md) | 简单 | | 509. [斐波那契数](../../problemset/fibonacci-number/README.md) | 简单 | @@ -355,6 +357,7 @@ | 题目 | 难度 | | ---- | ---- | | 90. [子集 II](../../problemset/subsets-ii/README.md) | 中等 | +| 171. [Excel 表列序号](../../problemset/excel-sheet-column-number/README.md) | 简单 | | 187. [重复的DNA序列](../../problemset/repeated-dna-sequences/README.md) | 中等 | | 191. [位1的个数](../../problemset/number-of-1-bits/README.md) | 简单 | | 476. [数字的补数](../../problemset/number-complement/README.md) | 简单 | diff --git a/assets/docs/TOPICS.md b/assets/docs/TOPICS.md index 4776dde..f455b3d 100644 --- a/assets/docs/TOPICS.md +++ b/assets/docs/TOPICS.md @@ -172,6 +172,8 @@ [169. 多数元素](../../problemset/majority-element/README.md) +[171. Excel 表列序号](../../problemset/excel-sheet-column-number/README.md) + [187. 重复的DNA序列](../../problemset/repeated-dna-sequences/README.md) [191. 位1的个数](../../problemset/number-of-1-bits/README.md) diff --git a/problemset/excel-sheet-column-number/README.md b/problemset/excel-sheet-column-number/README.md new file mode 100644 index 0000000..1814628 --- /dev/null +++ b/problemset/excel-sheet-column-number/README.md @@ -0,0 +1,83 @@ +# Excel 表列序号 + +> 难度:简单 +> +> https://leetcode.cn/problems/excel-sheet-column-number/description/ + +## 题目 + +给你一个字符串 `columnTitle` ,表示 `Excel` 表格中的列名称。返回 该列名称对应的列序号 。 + +例如: +```typescript +A -> 1 +B -> 2 +C -> 3 +... +Z -> 26 +AA -> 27 +AB -> 28 +... +``` + +### 示例 1: + +```typescript +输入: columnTitle = "A" +输出: 1 + +``` + +### 示例 2: + +```typescript +输入: columnTitle = "AB" +输出: 28 +``` + +### 示例 3: + +```typescript +输入: columnTitle = "ZY" +输出: 701 +``` + +## 题解 + +### typescript +```typescript +export function titleToNumber(columnTitle: string): number { + let res = 0; + const A = 'A'.charCodeAt(0) - 1; + const n = columnTitle.length; + for (let i = 0; i < n; i++) { + const c = columnTitle.charCodeAt(n - 1 - i); + // console.log(columnTitle[n - 1 - i], c) + res = res + (c - A) * Math.pow(26, i) + } + return res; +} + +// titleToNumber('ZY'); // AB ZY + +``` + +### python3 + +```python +import math +class Solution: + def titleToNumber(self, columnTitle: str) -> int: + res = 0; + _A = ord('A') - 1 + n = len(columnTitle) + # print(n) + for i in range(n): + c = ord(columnTitle[n - i - 1]) + res = res + (c -_A) * math.pow(26, i) + return int(res) +# s = Solution() +# print(s.titleToNumber('A')) +# print(s.titleToNumber('AB')) +# print(s.titleToNumber('ZY')) +``` diff --git a/problemset/excel-sheet-column-number/index.py b/problemset/excel-sheet-column-number/index.py new file mode 100644 index 0000000..ffc15e8 --- /dev/null +++ b/problemset/excel-sheet-column-number/index.py @@ -0,0 +1,16 @@ +import math +class Solution: + def titleToNumber(self, columnTitle: str) -> int: + res = 0; + _A = ord('A') - 1 + n = len(columnTitle) + # print(n) + for i in range(n): + c = ord(columnTitle[n - i - 1]) + res = res + (c -_A) * math.pow(26, i) + return int(res) + +# s = Solution() +# print(s.titleToNumber('A')) +# print(s.titleToNumber('AB')) +# print(s.titleToNumber('ZY')) diff --git a/problemset/excel-sheet-column-number/index.spec.ts b/problemset/excel-sheet-column-number/index.spec.ts new file mode 100644 index 0000000..a9f6f50 --- /dev/null +++ b/problemset/excel-sheet-column-number/index.spec.ts @@ -0,0 +1,18 @@ +import { describe, expect, it } from 'vitest' +import { titleToNumber } from '.' +describe('Excel 表列序号', () => { + describe('first', () => { + testCase(titleToNumber) + }) +}) + +function testCase(fn: (columnTitle: string) => number) { + it.each([ + // test cases + ['A', 1], + ['AB', 28], + ['ZY', 701], + ])('示例%#', (columnTitle, expected) => { + expect(fn(columnTitle)).toBe(expected) + }) +} diff --git a/problemset/excel-sheet-column-number/index.ts b/problemset/excel-sheet-column-number/index.ts new file mode 100644 index 0000000..8c2059b --- /dev/null +++ b/problemset/excel-sheet-column-number/index.ts @@ -0,0 +1,13 @@ +export function titleToNumber(columnTitle: string): number { + let res = 0; + const A = 'A'.charCodeAt(0) - 1; + const n = columnTitle.length; + for (let i = 0; i < n; i++) { + const c = columnTitle.charCodeAt(n - 1 - i); + // console.log(columnTitle[n - 1 - i], c) + res = res + (c - A) * Math.pow(26, i) + } + return res; +} + +// titleToNumber('ZY'); // AB ZY