Skip to content

Commit c8846c5

Browse files
committed
Add solution and test-cases for problem 2698
1 parent d569e0c commit c8846c5

File tree

3 files changed

+70
-9
lines changed

3 files changed

+70
-9
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# [2698.Find the Punishment Number of an Integer][title]
2+
3+
## Description
4+
Given a positive integer `n`, return the **punishment number** of `n`.
5+
6+
The **punishment number** of `n` is defined as the sum of the squares of all integers `i` such that:
7+
8+
- `1 <= i <= n`
9+
- The decimal representation of `i * i` can be partitioned into contiguous substrings such that the sum of the integer values of these substrings equals `i`.
10+
11+
**Example 1:**
12+
13+
```
14+
Input: n = 10
15+
Output: 182
16+
Explanation: There are exactly 3 integers i in the range [1, 10] that satisfy the conditions in the statement:
17+
- 1 since 1 * 1 = 1
18+
- 9 since 9 * 9 = 81 and 81 can be partitioned into 8 and 1 with a sum equal to 8 + 1 == 9.
19+
- 10 since 10 * 10 = 100 and 100 can be partitioned into 10 and 0 with a sum equal to 10 + 0 == 10.
20+
Hence, the punishment number of 10 is 1 + 81 + 100 = 182
21+
```
22+
23+
**Example 2:**
24+
25+
```
26+
Input: n = 37
27+
Output: 1478
28+
Explanation: There are exactly 4 integers i in the range [1, 37] that satisfy the conditions in the statement:
29+
- 1 since 1 * 1 = 1.
30+
- 9 since 9 * 9 = 81 and 81 can be partitioned into 8 + 1.
31+
- 10 since 10 * 10 = 100 and 100 can be partitioned into 10 + 0.
32+
- 36 since 36 * 36 = 1296 and 1296 can be partitioned into 1 + 29 + 6.
33+
Hence, the punishment number of 37 is 1 + 81 + 100 + 1296 = 1478
34+
```
35+
36+
## 结语
37+
38+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
39+
40+
[title]: https://leetcode.com/problems/find-the-punishment-number-of-an-integer
41+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func ok(n, target int) bool {
4+
if n == target {
5+
return true
6+
}
7+
base := 10
8+
for ; base <= n; base *= 10 {
9+
mod := n % base
10+
if ok(n/base, target-mod) {
11+
return true
12+
}
13+
}
14+
return false
15+
}
16+
func Solution(n int) int {
17+
ans := 0
18+
for i := 1; i <= n; i++ {
19+
x := i * i
20+
res := ok(x, i)
21+
if res {
22+
ans += x
23+
}
24+
}
25+
return ans
526
}

leetcode/2601-2700/2698.Find-the-Punishment-Number-of-an-Integer/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 10, 182},
17+
{"TestCase2", 37, 1478},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)