File tree Expand file tree Collapse file tree 10 files changed +116
-18
lines changed
determine-color-of-a-chessboard-square Expand file tree Collapse file tree 10 files changed +116
-18
lines changed Original file line number Diff line number Diff line change 2
2
3
3
<p align =" center " >
4
4
<!-- 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 " >
6
6
<!-- TOPICS COUNT END -->
7
7
<a href =" ./assets/docs/TOPICS.md " ><img src =" https://img.shields.io/badge/-题库目录-blue " alt =" 题库目录 " ></a >
8
8
<a href =" ./assets/docs/CATEGORIES.md " ><img src =" https://img.shields.io/badge/-题库分类-red " alt =" 题库分类 " ></a >
Original file line number Diff line number Diff line change 1407
1407
"path" : " ./problemset/find-center-of-star-graph/README.md" ,
1408
1408
"difficulty" : " 简单"
1409
1409
},
1410
+ {
1411
+ "id" : " 1812" ,
1412
+ "title" : " 判断国际象棋棋盘中一个格子的颜色" ,
1413
+ "path" : " ./problemset/determine-color-of-a-chessboard-square/README.md" ,
1414
+ "difficulty" : " 简单"
1415
+ },
1410
1416
{
1411
1417
"id" : " 1823" ,
1412
1418
"title" : " 找出游戏的获胜者" ,
Original file line number Diff line number Diff line change 5299
5299
"url" : " https://leetcode.cn/problems/number-of-different-integers-in-a-string/" ,
5300
5300
"path" : " ./problemset/number-of-different-integers-in-a-string/README.md"
5301
5301
},
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
+ },
5302
5312
{
5303
5313
"id" : " 1822" ,
5304
5314
"title" : {
5649
5659
"url" : " https://leetcode.cn/problems/zero-matrix-lcci/" ,
5650
5660
"path" : " ./problemset/zero-matrix-lcci/README.md"
5651
5661
}
5652
- ]
5662
+ ]
Original file line number Diff line number Diff line change 259
259
| 1704. [ 判断字符串的两半是否相似] ( ../../problemset/determine-if-string-halves-are-alike/README.md ) | 简单 |
260
260
| 1784. [ 检查二进制字符串字段] ( ../../problemset/check-if-binary-string-has-at-most-one-segment-of-ones/README.md ) | 简单 |
261
261
| 1791. [ 找出星型图的中心节点] ( ../../problemset/find-center-of-star-graph/README.md ) | 简单 |
262
+ | 1812. [ 判断国际象棋棋盘中一个格子的颜色] ( ../../problemset/determine-color-of-a-chessboard-square/README.md ) | 简单 |
262
263
| 1823. [ 找出游戏的获胜者] ( ../../problemset/find-the-winner-of-the-circular-game/README.md ) | 中等 |
263
264
264
265
## 自动机
Original file line number Diff line number Diff line change 1060
1060
1061
1061
[ 1805. 字符串中不同整数的数目] ( ../../problemset/number-of-different-integers-in-a-string/README.md )
1062
1062
1063
+ [ 1812. 判断国际象棋棋盘中一个格子的颜色] ( ../../problemset/determine-color-of-a-chessboard-square/README.md )
1064
+
1063
1065
[ 1822. 数组元素积的符号] ( ../../problemset/sign-of-the-product-of-an-array/README.md )
1064
1066
1065
1067
[ 1823. 找出游戏的获胜者] ( ../../problemset/find-the-winner-of-the-circular-game/README.md )
Original file line number Diff line number Diff line change 37
37
/**
38
38
* 数学
39
39
* @desc 时间复杂度 O(NlogN) 空间复杂度 O(logN)
40
- * @param nums
41
- * @returns
40
+ * @param nums
41
+ * @returns
42
42
*/
43
43
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
+ }
55
55
```
Original file line number Diff line number Diff line change
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
+ ```
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 31
31
``` ts
32
32
/**
33
33
* 广度优先遍历
34
- * @param s1
35
- * @param s2
36
- * @returns
34
+ * @param s1
35
+ * @param s2
36
+ * @returns
37
37
*/
38
38
export function kSimilarity(s1 : string , s2 : string ): number {
39
39
const n = s1 .length
You can’t perform that action at this time.
0 commit comments