Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c7bdf30

Browse filesBrowse files
authored
Merge pull request #1195 from 0xff-dev/970
Add solution and test-cases for problem 970
2 parents c8132bd + 4733403 commit c7bdf30
Copy full SHA for c7bdf30

File tree

Expand file treeCollapse file tree

3 files changed

+66
-27
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+66
-27
lines changed

‎leetcode/901-1000/0970.Powerful-Integers/README.md

Copy file name to clipboardExpand all lines: leetcode/901-1000/0970.Powerful-Integers/README.md
+19-14Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
# [970.Powerful Integers][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given three integers `x`, `y`, and `bound`, return a list of all the **powerful integers** that have a value less than or equal to `bound`.
5+
6+
An integer is **powerful** if it can be represented as `xi + yj` for some integers `i >= 0` and `j >= 0`.
7+
8+
You may return the answer in **any order**. In your answer, each value should occur **at most once**.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: x = 2, y = 3, bound = 10
14+
Output: [2,3,4,5,7,9,10]
15+
Explanation:
16+
2 = 2^0 + 3^0
17+
3 = 2^1 + 3^0
18+
4 = 2^0 + 3^1
19+
5 = 2^1 + 3^1
20+
7 = 2^2 + 3^1
21+
9 = 2^3 + 3^0
22+
10 = 2^0 + 3^2
1323
```
1424

15-
## 题意
16-
> ...
25+
**Example 2:**
1726

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Powerful Integers
23-
```go
2427
```
25-
28+
Input: x = 3, y = 5, bound = 15
29+
Output: [2,4,6,8,10,14]
30+
```
2631

2732
## 结语
2833

+35-2Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(x int, y int, bound int) []int {
4+
xm := map[int]struct{}{
5+
1: struct{}{},
6+
}
7+
ym := map[int]struct{}{
8+
1: struct{}{},
9+
}
10+
if x != 1 {
11+
base := x
12+
for base <= bound {
13+
xm[base] = struct{}{}
14+
base *= x
15+
}
16+
}
17+
if y != 1 {
18+
base := 1
19+
for base <= bound {
20+
ym[base] = struct{}{}
21+
base *= y
22+
}
23+
}
24+
25+
ans := make([]int, 0)
26+
exist := make(map[int]struct{})
27+
for xi := range xm {
28+
for yi := range ym {
29+
if r := xi + yi; r <= bound {
30+
if _, ok := exist[r]; !ok {
31+
exist[r] = struct{}{}
32+
ans = append(ans, r)
33+
}
34+
}
35+
}
36+
}
37+
return ans
538
}

‎leetcode/901-1000/0970.Powerful-Integers/Solution_test.go

Copy file name to clipboardExpand all lines: leetcode/901-1000/0970.Powerful-Integers/Solution_test.go
+12-11Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,39 @@ package Solution
22

33
import (
44
"reflect"
5+
"sort"
56
"strconv"
67
"testing"
78
)
89

910
func TestSolution(t *testing.T) {
1011
// 测试用例
1112
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
13+
name string
14+
x, y, bound int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 2, 3, 10, []int{2, 3, 4, 5, 7, 9, 10}},
18+
{"TestCase2", 3, 5, 15, []int{2, 4, 6, 8, 10, 14}},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.x, c.y, c.bound)
25+
sort.Ints(got)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
28+
c.expect, got, c.x, c.y, c.bound)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.