Skip to content

Commit 63dcb40

Browse files
committed
Add solution and test-cases for problem 1317
1 parent df4288a commit 63dcb40

File tree

3 files changed

+50
-23
lines changed

3 files changed

+50
-23
lines changed

leetcode/1301-1400/1317.Convert-Integer-to-the-Sum-of-Two-No-Zero-Integers/README.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
# [1317.Convert Integer to the Sum of Two No-Zero 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+
**No-Zero integer** is a positive integer that **does not contain any** `0` in its decimal representation.
5+
6+
Given an integer `n`, return a list of two integers `[a, b]` where:
7+
8+
- `a` and `b` are **No-Zero integers**.
9+
- `a + b = n`
10+
11+
The test cases are generated so that there is at least one valid solution. If there are many valid solutions, you can return any of them.
712

813
**Example 1:**
914

1015
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
16+
Input: n = 2
17+
Output: [1,1]
18+
Explanation: Let a = 1 and b = 1.
19+
Both a and b are no-zero integers, and a + b = 2 = n.
1320
```
1421

15-
## 题意
16-
> ...
22+
**Example 2:**
1723

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Convert Integer to the Sum of Two No-Zero Integers
23-
```go
2424
```
25-
25+
Input: n = 11
26+
Output: [2,9]
27+
Explanation: Let a = 2 and b = 9.
28+
Both a and b are no-zero integers, and a + b = 11 = n.
29+
Note that there are other valid answers as [8, 3] that can be accepted.
30+
```
2631

2732
## 结语
2833

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

3-
func Solution(x bool) bool {
4-
return x
3+
func ok(n int) bool {
4+
for n > 0 {
5+
mod := n % 10
6+
if mod == 0 {
7+
return false
8+
}
9+
n /= 10
10+
}
11+
return true
12+
}
13+
14+
func Solution(n int) []int {
15+
nums := make(map[int]struct{})
16+
for i := 1; i <= 10000; i++ {
17+
if ok(i) {
18+
nums[i] = struct{}{}
19+
}
20+
}
21+
var diff int
22+
for k := range nums {
23+
diff = n - k
24+
if _, ok := nums[diff]; ok {
25+
return []int{k, diff}
26+
}
27+
}
28+
return []int{}
529
}

leetcode/1301-1400/1317.Convert-Integer-to-the-Sum-of-Two-No-Zero-Integers/Solution_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ 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", 2, []int{1, 1}},
1917
}
2018

2119
// 开始测试
@@ -30,10 +28,10 @@ func TestSolution(t *testing.T) {
3028
}
3129
}
3230

33-
// 压力测试
31+
// 压力测试
3432
func BenchmarkSolution(b *testing.B) {
3533
}
3634

37-
// 使用案列
35+
// 使用案列
3836
func ExampleSolution() {
3937
}

0 commit comments

Comments
 (0)