Skip to content

Commit 728c73a

Browse files
committed
feat: leetcode 1812
1 parent 20c932f commit 728c73a

File tree

10 files changed

+116
-18
lines changed

10 files changed

+116
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<p align="center">
44
<!-- TOPICS COUNT START -->
5-
<img src="https://img.shields.io/badge/-进度:565-green" alt="进度:565">
5+
<img src="https://img.shields.io/badge/-进度:566-green" alt="进度:566">
66
<!-- TOPICS COUNT END -->
77
<a href="./assets/docs/TOPICS.md"><img src="https://img.shields.io/badge/-题库目录-blue" alt="题库目录"></a>
88
<a href="./assets/docs/CATEGORIES.md"><img src="https://img.shields.io/badge/-题库分类-red" alt="题库分类"></a>

assets/data/categories.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,6 +1407,12 @@
14071407
"path": "./problemset/find-center-of-star-graph/README.md",
14081408
"difficulty": "简单"
14091409
},
1410+
{
1411+
"id": "1812",
1412+
"title": "判断国际象棋棋盘中一个格子的颜色",
1413+
"path": "./problemset/determine-color-of-a-chessboard-square/README.md",
1414+
"difficulty": "简单"
1415+
},
14101416
{
14111417
"id": "1823",
14121418
"title": "找出游戏的获胜者",

assets/data/topics.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5299,6 +5299,16 @@
52995299
"url": "https://leetcode.cn/problems/number-of-different-integers-in-a-string/",
53005300
"path": "./problemset/number-of-different-integers-in-a-string/README.md"
53015301
},
5302+
{
5303+
"id": "1812",
5304+
"title": {
5305+
"cn": "判断国际象棋棋盘中一个格子的颜色",
5306+
"en": "determine-color-of-a-chessboard-square"
5307+
},
5308+
"difficulty": "简单",
5309+
"url": "https://leetcode.cn/problems/determine-color-of-a-chessboard-square/",
5310+
"path": "./problemset/determine-color-of-a-chessboard-square/README.md"
5311+
},
53025312
{
53035313
"id": "1822",
53045314
"title": {
@@ -5649,4 +5659,4 @@
56495659
"url": "https://leetcode.cn/problems/zero-matrix-lcci/",
56505660
"path": "./problemset/zero-matrix-lcci/README.md"
56515661
}
5652-
]
5662+
]

assets/docs/CATEGORIES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@
259259
| 1704. [判断字符串的两半是否相似](../../problemset/determine-if-string-halves-are-alike/README.md) | 简单 |
260260
| 1784. [检查二进制字符串字段](../../problemset/check-if-binary-string-has-at-most-one-segment-of-ones/README.md) | 简单 |
261261
| 1791. [找出星型图的中心节点](../../problemset/find-center-of-star-graph/README.md) | 简单 |
262+
| 1812. [判断国际象棋棋盘中一个格子的颜色](../../problemset/determine-color-of-a-chessboard-square/README.md) | 简单 |
262263
| 1823. [找出游戏的获胜者](../../problemset/find-the-winner-of-the-circular-game/README.md) | 中等 |
263264

264265
## 自动机

assets/docs/TOPICS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,8 @@
10601060

10611061
[1805. 字符串中不同整数的数目](../../problemset/number-of-different-integers-in-a-string/README.md)
10621062

1063+
[1812. 判断国际象棋棋盘中一个格子的颜色](../../problemset/determine-color-of-a-chessboard-square/README.md)
1064+
10631065
[1822. 数组元素积的符号](../../problemset/sign-of-the-product-of-an-array/README.md)
10641066

10651067
[1823. 找出游戏的获胜者](../../problemset/find-the-winner-of-the-circular-game/README.md)

problemset/description/README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@
3737
/**
3838
* 数学
3939
* @desc 时间复杂度 O(NlogN) 空间复杂度 O(logN)
40-
* @param nums
41-
* @returns
40+
* @param nums
41+
* @returns
4242
*/
4343
export function sumSubseqWidths(nums: number[]): number {
44-
const MOD = 1000000007;
45-
nums.sort((a, b) => a - b);
46-
let res = 0;
47-
let x = nums[0], y = 2;
48-
for (let j = 1; j < nums.length; j++) {
49-
res = (res + nums[j] * (y - 1) - x) % MOD;
50-
x = (x * 2 + nums[j]) % MOD;
51-
y = y * 2 % MOD;
52-
}
53-
return (res + MOD) % MOD;
54-
};
44+
const MOD = 1000000007
45+
nums.sort((a, b) => a - b)
46+
let res = 0
47+
let x = nums[0]; let y = 2
48+
for (let j = 1; j < nums.length; j++) {
49+
res = (res + nums[j] * (y - 1) - x) % MOD
50+
x = (x * 2 + nums[j]) % MOD
51+
y = y * 2 % MOD
52+
}
53+
return (res + MOD) % MOD
54+
}
5555
```
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# 判断国际象棋棋盘中一个格子的颜色
2+
3+
> 难度:简单
4+
>
5+
> https://leetcode.cn/problems/determine-color-of-a-chessboard-square/
6+
7+
## 题目
8+
9+
给你一个坐标 `coordinates` ,它是一个字符串,表示国际象棋棋盘中一个格子的坐标。下图是国际象棋棋盘示意图。
10+
11+
![image](https://user-images.githubusercontent.com/54696834/206342937-baff0119-7739-4588-ae89-dcbf6d22cebf.png)
12+
13+
如果所给格子的颜色是白色,请你返回 `true`,如果是黑色,请返回 `false`
14+
15+
给定坐标一定代表国际象棋棋盘上一个存在的格子。坐标第一个字符是字母,第二个字符是数字。
16+
17+
### 示例
18+
19+
#### 示例 1:
20+
21+
```
22+
输入:coordinates = "a1"
23+
输出:false
24+
解释:如上图棋盘所示,"a1" 坐标的格子是黑色的,所以返回 false 。
25+
```
26+
27+
#### 示例 2:
28+
29+
```
30+
输入:coordinates = "h3"
31+
输出:true
32+
解释:如上图棋盘所示,"h3" 坐标的格子是白色的,所以返回 true 。
33+
```
34+
35+
#### 示例 3:
36+
37+
```
38+
输入:coordinates = "c7"
39+
输出:false
40+
```
41+
42+
## 解题
43+
44+
```ts
45+
/**
46+
* 数学
47+
* @desc 时间复杂度 O(1) 空间复杂度 O(1)
48+
* @param coordinates
49+
* @returns
50+
*/
51+
export function squareIsWhite(coordinates: string): boolean {
52+
return ((coordinates[0].charCodeAt(0) - 'a'.charCodeAt(0) + 1) + (coordinates[1].charCodeAt(0) - '0'.charCodeAt(0))) % 2 === 1
53+
}
54+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { squareIsWhite } from '.'
3+
4+
describe('判断国际象棋棋盘中一个格子的颜色', () => {
5+
testCase(squareIsWhite)
6+
})
7+
8+
function testCase(fn: (coordinates: string) => boolean) {
9+
it.each([
10+
['a1', false],
11+
['h3', true],
12+
['c7', false],
13+
])('示例%#', (corrdinates, expected) => {
14+
expect(fn(corrdinates)).toBe(expected)
15+
})
16+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* 数学
3+
* @desc 时间复杂度 O(1) 空间复杂度 O(1)
4+
* @param coordinates
5+
* @returns
6+
*/
7+
export function squareIsWhite(coordinates: string): boolean {
8+
return ((coordinates[0].charCodeAt(0) - 'a'.charCodeAt(0) + 1) + (coordinates[1].charCodeAt(0) - '0'.charCodeAt(0))) % 2 === 1
9+
}

problemset/k-similar-strings/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
```ts
3232
/**
3333
* 广度优先遍历
34-
* @param s1
35-
* @param s2
36-
* @returns
34+
* @param s1
35+
* @param s2
36+
* @returns
3737
*/
3838
export function kSimilarity(s1: string, s2: string): number {
3939
const n = s1.length

0 commit comments

Comments
 (0)