File tree Expand file tree Collapse file tree 8 files changed +104
-2
lines changed
problemset/check-if-number-is-a-sum-of-powers-of-three Expand file tree Collapse file tree 8 files changed +104
-2
lines changed Original file line number Diff line number Diff line change 22
33<p align =" center " >
44<!-- 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 " >
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 >
Original file line number Diff line number Diff line change 13951395 "path" : " ./problemset/determine-if-string-halves-are-alike/README.md" ,
13961396 "difficulty" : " 简单"
13971397 },
1398+ {
1399+ "id" : " 1780" ,
1400+ "title" : " 判断一个数字是否可以表示成三的幂的和" ,
1401+ "path" : " ./problemset/check-if-number-is-a-sum-of-powers-of-three/README.md" ,
1402+ "difficulty" : " 中等"
1403+ },
13981404 {
13991405 "id" : " 1784" ,
14001406 "title" : " 检查二进制字符串字段" ,
Original file line number Diff line number Diff line change 52395239 "url" : " https://leetcode.cn/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/" ,
52405240 "path" : " ./problemset/find-nearest-point-that-has-the-same-x-or-y-coordinate/README.md"
52415241 },
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+ },
52425252 {
52435253 "id" : " 1784" ,
52445254 "title" : {
56595669 "url" : " https://leetcode.cn/problems/zero-matrix-lcci/" ,
56605670 "path" : " ./problemset/zero-matrix-lcci/README.md"
56615671 }
5662- ]
5672+ ]
Original file line number Diff line number Diff line change 257257| 1175. [ 质数排列] ( ../../problemset/prime-arrangements/README.md ) | 简单 |
258258| 1447. [ 最简分数] ( ../../problemset/simplified-fractions/README.md ) | 中等 |
259259| 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 ) | 中等 |
260261| 1784. [ 检查二进制字符串字段] ( ../../problemset/check-if-binary-string-has-at-most-one-segment-of-ones/README.md ) | 简单 |
261262| 1791. [ 找出星型图的中心节点] ( ../../problemset/find-center-of-star-graph/README.md ) | 简单 |
262263| 1812. [ 判断国际象棋棋盘中一个格子的颜色] ( ../../problemset/determine-color-of-a-chessboard-square/README.md ) | 简单 |
Original file line number Diff line number Diff line change 10481048
10491049[ 1779. 找到最近的有相同 X 或 Y 坐标的点] ( ../../problemset/find-nearest-point-that-has-the-same-x-or-y-coordinate/README.md )
10501050
1051+ [ 1780. 判断一个数字是否可以表示成三的幂的和] ( ../../problemset/check-if-number-is-a-sum-of-powers-of-three/README.md )
1052+
10511053[ 1784. 检查二进制字符串字段] ( ../../problemset/check-if-binary-string-has-at-most-one-segment-of-ones/README.md )
10521054
10531055[ 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