Skip to content

Commit

Permalink
feat: leetcode 171
Browse files Browse the repository at this point in the history
  • Loading branch information
王洋 committed Jan 15, 2024
1 parent c2c04b0 commit e6aa581
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<p align="center">
<!-- TOPICS COUNT START -->
<img src="https://img.shields.io/badge/-进度:177-green" alt="进度:177">
<img src="https://img.shields.io/badge/-进度:178-green" alt="进度:178">
<!-- TOPICS COUNT END -->
<a href="./assets/docs/TOPICS.md"><img src="https://img.shields.io/badge/-题库目录-blue" alt="题库目录"></a>
<a href="./assets/docs/CATEGORIES.md"><img src="https://img.shields.io/badge/-题库分类-red" alt="题库分类"></a>
Expand Down
18 changes: 18 additions & 0 deletions assets/data/categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 游戏",
Expand Down Expand Up @@ -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序列",
Expand Down
10 changes: 10 additions & 0 deletions assets/data/topics.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
3 changes: 3 additions & 0 deletions assets/docs/CATEGORIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | 中等 |
Expand Down Expand Up @@ -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) | 简单 |
Expand Down Expand Up @@ -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) | 简单 |
Expand Down
2 changes: 2 additions & 0 deletions assets/docs/TOPICS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
83 changes: 83 additions & 0 deletions problemset/excel-sheet-column-number/README.md
Original file line number Diff line number Diff line change
@@ -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'))
```
16 changes: 16 additions & 0 deletions problemset/excel-sheet-column-number/index.py
Original file line number Diff line number Diff line change
@@ -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'))
18 changes: 18 additions & 0 deletions problemset/excel-sheet-column-number/index.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})
}
13 changes: 13 additions & 0 deletions problemset/excel-sheet-column-number/index.ts
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e6aa581

Please sign in to comment.