Skip to content

Commit 9db7bde

Browse files
committed
[E:48/531, M:52/969, H:6/386] add No: 326 Power of Three
1 parent 39a51aa commit 9db7bde

File tree

9 files changed

+297
-1
lines changed

9 files changed

+297
-1
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
## [3的幂](https://leetcode-cn.com/problems/power-of-three/)
2+
3+
给定一个整数,写一个函数来判断它是否是 3 的幂次方。如果是,返回 `true` ;否则,返回 `false`
4+
5+
整数 `n` 是 3 的幂次方需满足:存在整数 `x` 使得 `n == 3
6+
x
7+
`
8+
9+
 
10+
11+
**示例 1:**
12+
13+
`
14+
**输入:**n = 27
15+
**输出:**true
16+
`
17+
18+
**示例 2:**
19+
20+
`
21+
**输入:**n = 0
22+
**输出:**false
23+
`
24+
25+
**示例 3:**
26+
27+
`
28+
**输入:**n = 9
29+
**输出:**true
30+
`
31+
32+
**示例 4:**
33+
34+
`
35+
**输入:**n = 45
36+
**输出:**false
37+
`
38+
39+
 
40+
41+
**提示:**
42+
43+
* `-2
44+
31
45+
46+
31
47+
- 1`
48+
49+
 
50+
51+
**进阶:**
52+
53+
* 你能不使用循环或者递归来完成本题吗?
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/简单/326/golang/solution"
11+
)
12+
13+
func main() {
14+
15+
tests := []struct {
16+
name string
17+
input int
18+
want bool
19+
}{
20+
{
21+
name: "test-27",
22+
input: 27,
23+
want: true,
24+
},
25+
{
26+
name: "test-9",
27+
input: 9,
28+
want: true,
29+
},
30+
{
31+
name: "test-0",
32+
input: 0,
33+
want: false,
34+
},
35+
{
36+
name: "test-1",
37+
input: 1,
38+
want: true,
39+
},
40+
}
41+
42+
testLog := leet.NewTestLog(len(tests))
43+
defer testLog.Render()
44+
45+
timeoutDuration := time.Second * 2
46+
47+
for idx, test := range tests {
48+
// 超时检测
49+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
50+
solution.Export(test.input)
51+
cancel()
52+
})
53+
54+
if timeout {
55+
testLog.Fail(idx+1, test.name, "timeout")
56+
continue
57+
}
58+
59+
got := solution.Export(test.input)
60+
if !reflect.DeepEqual(test.want, got) {
61+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
62+
continue
63+
}
64+
65+
testLog.Pass(idx+1, test.name)
66+
}
67+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(n int) bool {
4+
return isPowerOfThree(n)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func isPowerOfThree(n int) bool {
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(n int) bool {
4+
return isPowerOfThree(n)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func isPowerOfThree(n int) bool {
13+
14+
}

questions/store.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
## [3的幂](https://leetcode-cn.com/problems/power-of-three/)
2+
3+
给定一个整数,写一个函数来判断它是否是 3 的幂次方。如果是,返回 `true` ;否则,返回 `false`
4+
5+
整数 `n` 是 3 的幂次方需满足:存在整数 `x` 使得 `n == 3
6+
x
7+
`
8+
9+
 
10+
11+
**示例 1:**
12+
13+
`
14+
**输入:**n = 27
15+
**输出:**true
16+
`
17+
18+
**示例 2:**
19+
20+
`
21+
**输入:**n = 0
22+
**输出:**false
23+
`
24+
25+
**示例 3:**
26+
27+
`
28+
**输入:**n = 9
29+
**输出:**true
30+
`
31+
32+
**示例 4:**
33+
34+
`
35+
**输入:**n = 45
36+
**输出:**false
37+
`
38+
39+
 
40+
41+
**提示:**
42+
43+
* `-2
44+
31
45+
46+
31
47+
- 1`
48+
49+
 
50+
51+
**进阶:**
52+
53+
* 你能不使用循环或者递归来完成本题吗?
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/简单/326/golang/solution"
11+
)
12+
13+
func main() {
14+
15+
tests := []struct {
16+
name string
17+
input int
18+
want bool
19+
}{
20+
{
21+
name: "test-27",
22+
input: 27,
23+
want: true,
24+
},
25+
{
26+
name: "test-9",
27+
input: 9,
28+
want: true,
29+
},
30+
{
31+
name: "test-0",
32+
input: 0,
33+
want: false,
34+
},
35+
{
36+
name: "test-1",
37+
input: 1,
38+
want: true,
39+
},
40+
}
41+
42+
testLog := leet.NewTestLog(len(tests))
43+
defer testLog.Render()
44+
45+
timeoutDuration := time.Second * 2
46+
47+
for idx, test := range tests {
48+
// 超时检测
49+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
50+
solution.Export(test.input)
51+
cancel()
52+
})
53+
54+
if timeout {
55+
testLog.Fail(idx+1, test.name, "timeout")
56+
continue
57+
}
58+
59+
got := solution.Export(test.input)
60+
if !reflect.DeepEqual(test.want, got) {
61+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
62+
continue
63+
}
64+
65+
testLog.Pass(idx+1, test.name)
66+
}
67+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(n int) bool {
4+
return isPowerOfThree(n)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func isPowerOfThree(n int) bool {
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(n int) bool {
4+
return isPowerOfThree(n)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func isPowerOfThree(n int) bool {
13+
14+
}

0 commit comments

Comments
 (0)