File tree 8 files changed +104
-2
lines changed
problemset/check-if-number-is-a-sum-of-powers-of-three
8 files changed +104
-2
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/-进度:566 -green " alt =" 进度:566 " >
5
+ <img src =" https://img.shields.io/badge/-进度:567 -green " alt =" 进度:567 " >
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 1395
1395
"path" : " ./problemset/determine-if-string-halves-are-alike/README.md" ,
1396
1396
"difficulty" : " 简单"
1397
1397
},
1398
+ {
1399
+ "id" : " 1780" ,
1400
+ "title" : " 判断一个数字是否可以表示成三的幂的和" ,
1401
+ "path" : " ./problemset/check-if-number-is-a-sum-of-powers-of-three/README.md" ,
1402
+ "difficulty" : " 中等"
1403
+ },
1398
1404
{
1399
1405
"id" : " 1784" ,
1400
1406
"title" : " 检查二进制字符串字段" ,
Original file line number Diff line number Diff line change 5239
5239
"url" : " https://leetcode.cn/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/" ,
5240
5240
"path" : " ./problemset/find-nearest-point-that-has-the-same-x-or-y-coordinate/README.md"
5241
5241
},
5242
+ {
5243
+ "id" : " 1780" ,
5244
+ "title" : {
5245
+ "cn" : " 判断一个数字是否可以表示成三的幂的和" ,
5246
+ "en" : " check-if-number-is-a-sum-of-powers-of-three"
5247
+ },
5248
+ "difficulty" : " 中等" ,
5249
+ "url" : " https://leetcode.cn/problems/check-if-number-is-a-sum-of-powers-of-three/" ,
5250
+ "path" : " ./problemset/check-if-number-is-a-sum-of-powers-of-three/README.md"
5251
+ },
5242
5252
{
5243
5253
"id" : " 1784" ,
5244
5254
"title" : {
5659
5669
"url" : " https://leetcode.cn/problems/zero-matrix-lcci/" ,
5660
5670
"path" : " ./problemset/zero-matrix-lcci/README.md"
5661
5671
}
5662
- ]
5672
+ ]
Original file line number Diff line number Diff line change 257
257
| 1175. [ 质数排列] ( ../../problemset/prime-arrangements/README.md ) | 简单 |
258
258
| 1447. [ 最简分数] ( ../../problemset/simplified-fractions/README.md ) | 中等 |
259
259
| 1704. [ 判断字符串的两半是否相似] ( ../../problemset/determine-if-string-halves-are-alike/README.md ) | 简单 |
260
+ | 1780. [ 判断一个数字是否可以表示成三的幂的和] ( ../../problemset/check-if-number-is-a-sum-of-powers-of-three/README.md ) | 中等 |
260
261
| 1784. [ 检查二进制字符串字段] ( ../../problemset/check-if-binary-string-has-at-most-one-segment-of-ones/README.md ) | 简单 |
261
262
| 1791. [ 找出星型图的中心节点] ( ../../problemset/find-center-of-star-graph/README.md ) | 简单 |
262
263
| 1812. [ 判断国际象棋棋盘中一个格子的颜色] ( ../../problemset/determine-color-of-a-chessboard-square/README.md ) | 简单 |
Original file line number Diff line number Diff line change 1048
1048
1049
1049
[ 1779. 找到最近的有相同 X 或 Y 坐标的点] ( ../../problemset/find-nearest-point-that-has-the-same-x-or-y-coordinate/README.md )
1050
1050
1051
+ [ 1780. 判断一个数字是否可以表示成三的幂的和] ( ../../problemset/check-if-number-is-a-sum-of-powers-of-three/README.md )
1052
+
1051
1053
[ 1784. 检查二进制字符串字段] ( ../../problemset/check-if-binary-string-has-at-most-one-segment-of-ones/README.md )
1052
1054
1053
1055
[ 1790. 仅执行一次字符串交换能否使两个字符串相等] ( ../../problemset/check-if-one-string-swap-can-make-strings-equal/README.md )
Original file line number Diff line number Diff line change
1
+ # 判断一个数字是否可以表示成三的幂的和
2
+
3
+ > 难度:中等
4
+ >
5
+ > https://leetcode.cn/problems/check-if-number-is-a-sum-of-powers-of-three/
6
+
7
+ ## 题目
8
+
9
+ 给你一个整数 ` n ` ,如果你可以将 ` n ` 表示成若干个不同的三的幂之和,请你返回 ` true ` ,否则请返回 ` false ` 。
10
+
11
+ 对于一个整数 ` y ` ,如果存在整数 ` x ` 满足 ` y == 3^x ` ,我们称这个整数 ` y ` 是三的幂。
12
+
13
+ ### 示例
14
+
15
+ #### 示例 1:
16
+
17
+ ```
18
+ 输入:n = 12
19
+ 输出:true
20
+ 解释:12 = 3^1 + 3^2
21
+ ```
22
+
23
+ #### 示例 2:
24
+
25
+ ```
26
+ 输入:n = 91
27
+ 输出:true
28
+ 解释:91 = 3^0 + 3^2 + 3^4
29
+ ```
30
+
31
+ #### 示例 3:
32
+
33
+ ```
34
+ 输入:n = 21
35
+ 输出:false
36
+ ```
37
+
38
+ ## 解题
39
+
40
+ ``` ts
41
+ /**
42
+ * 进制
43
+ * @desc 时间复杂度 O(logN) 空间复杂度 O(1)
44
+ * @param n
45
+ * @returns
46
+ */
47
+ export function checkPowersOfThree(n : number ): boolean {
48
+ while (n !== 0 ) {
49
+ if (n % 3 === 2 ) return false
50
+ n = (n / 3 ) >> 0
51
+ }
52
+ return true
53
+ }
54
+ ```
Original file line number Diff line number Diff line change
1
+ import { describe , expect , it } from 'vitest'
2
+ import { checkPowersOfThree } from '.'
3
+
4
+ describe ( '判断一个数字是否可以表示成三的幂的和' , ( ) => {
5
+ testCase ( checkPowersOfThree )
6
+ } )
7
+
8
+ function testCase ( fn : ( n : number ) => boolean ) {
9
+ it . each ( [
10
+ [ 12 , true ] ,
11
+ [ 91 , true ] ,
12
+ [ 21 , false ] ,
13
+ ] ) ( '示例%#' , ( n , expected ) => {
14
+ expect ( fn ( n ) ) . toBe ( expected )
15
+ } )
16
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 进制
3
+ * @desc 时间复杂度 O(logN) 空间复杂度 O(1)
4
+ * @param n
5
+ * @returns
6
+ */
7
+ export function checkPowersOfThree ( n : number ) : boolean {
8
+ while ( n !== 0 ) {
9
+ if ( n % 3 === 2 ) return false
10
+ n = ( n / 3 ) >> 0
11
+ }
12
+ return true
13
+ }
You can’t perform that action at this time.
0 commit comments