Skip to content

Add solution and test-cases for problem 970 #1195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions leetcode/901-1000/0970.Powerful-Integers/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
# [970.Powerful Integers][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
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`.

An integer is **powerful** if it can be represented as `xi + yj` for some integers `i >= 0` and `j >= 0`.

You may return the answer in **any order**. In your answer, each value should occur **at most once**.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: x = 2, y = 3, bound = 10
Output: [2,3,4,5,7,9,10]
Explanation:
2 = 2^0 + 3^0
3 = 2^1 + 3^0
4 = 2^0 + 3^1
5 = 2^1 + 3^1
7 = 2^2 + 3^1
9 = 2^3 + 3^0
10 = 2^0 + 3^2
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Powerful Integers
```go
```

Input: x = 3, y = 5, bound = 15
Output: [2,4,6,8,10,14]
```

## 结语

Expand Down
37 changes: 35 additions & 2 deletions leetcode/901-1000/0970.Powerful-Integers/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(x int, y int, bound int) []int {
xm := map[int]struct{}{
1: struct{}{},
}
ym := map[int]struct{}{
1: struct{}{},
}
if x != 1 {
base := x
for base <= bound {
xm[base] = struct{}{}
base *= x
}
}
if y != 1 {
base := 1
for base <= bound {
ym[base] = struct{}{}
base *= y
}
}

ans := make([]int, 0)
exist := make(map[int]struct{})
for xi := range xm {
for yi := range ym {
if r := xi + yi; r <= bound {
if _, ok := exist[r]; !ok {
exist[r] = struct{}{}
ans = append(ans, r)
}
}
}
}
return ans
}
23 changes: 12 additions & 11 deletions leetcode/901-1000/0970.Powerful-Integers/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,39 @@ package Solution

import (
"reflect"
"sort"
"strconv"
"testing"
)

func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
x, y, bound int
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 2, 3, 10, []int{2, 3, 4, 5, 7, 9, 10}},
{"TestCase2", 3, 5, 15, []int{2, 4, 6, 8, 10, 14}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.x, c.y, c.bound)
sort.Ints(got)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
c.expect, got, c.x, c.y, c.bound)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading