From 63dcb40a53d1349ff50e2bd48a6e57f1c0d73668 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Mon, 8 Sep 2025 08:50:39 +0800 Subject: [PATCH] Add solution and test-cases for problem 1317 --- .../README.md | 33 +++++++++++-------- .../Solution.go | 28 ++++++++++++++-- .../Solution_test.go | 12 +++---- 3 files changed, 50 insertions(+), 23 deletions(-) diff --git a/leetcode/1301-1400/1317.Convert-Integer-to-the-Sum-of-Two-No-Zero-Integers/README.md b/leetcode/1301-1400/1317.Convert-Integer-to-the-Sum-of-Two-No-Zero-Integers/README.md index fd8546c1c..d735b2cc5 100644 --- a/leetcode/1301-1400/1317.Convert-Integer-to-the-Sum-of-Two-No-Zero-Integers/README.md +++ b/leetcode/1301-1400/1317.Convert-Integer-to-the-Sum-of-Two-No-Zero-Integers/README.md @@ -1,28 +1,33 @@ # [1317.Convert Integer to the Sum of Two No-Zero 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 +**No-Zero integer** is a positive integer that **does not contain any** `0` in its decimal representation. + +Given an integer `n`, return a list of two integers `[a, b]` where: + +- `a` and `b` are **No-Zero integers**. +- `a + b = n` + +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. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: n = 2 +Output: [1,1] +Explanation: Let a = 1 and b = 1. +Both a and b are no-zero integers, and a + b = 2 = n. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Convert Integer to the Sum of Two No-Zero Integers -```go ``` - +Input: n = 11 +Output: [2,9] +Explanation: Let a = 2 and b = 9. +Both a and b are no-zero integers, and a + b = 11 = n. +Note that there are other valid answers as [8, 3] that can be accepted. +``` ## 结语 diff --git a/leetcode/1301-1400/1317.Convert-Integer-to-the-Sum-of-Two-No-Zero-Integers/Solution.go b/leetcode/1301-1400/1317.Convert-Integer-to-the-Sum-of-Two-No-Zero-Integers/Solution.go index d115ccf5e..a82269088 100644 --- a/leetcode/1301-1400/1317.Convert-Integer-to-the-Sum-of-Two-No-Zero-Integers/Solution.go +++ b/leetcode/1301-1400/1317.Convert-Integer-to-the-Sum-of-Two-No-Zero-Integers/Solution.go @@ -1,5 +1,29 @@ package Solution -func Solution(x bool) bool { - return x +func ok(n int) bool { + for n > 0 { + mod := n % 10 + if mod == 0 { + return false + } + n /= 10 + } + return true +} + +func Solution(n int) []int { + nums := make(map[int]struct{}) + for i := 1; i <= 10000; i++ { + if ok(i) { + nums[i] = struct{}{} + } + } + var diff int + for k := range nums { + diff = n - k + if _, ok := nums[diff]; ok { + return []int{k, diff} + } + } + return []int{} } diff --git a/leetcode/1301-1400/1317.Convert-Integer-to-the-Sum-of-Two-No-Zero-Integers/Solution_test.go b/leetcode/1301-1400/1317.Convert-Integer-to-the-Sum-of-Two-No-Zero-Integers/Solution_test.go index 14ff50eb4..50d40778a 100644 --- a/leetcode/1301-1400/1317.Convert-Integer-to-the-Sum-of-Two-No-Zero-Integers/Solution_test.go +++ b/leetcode/1301-1400/1317.Convert-Integer-to-the-Sum-of-Two-No-Zero-Integers/Solution_test.go @@ -10,12 +10,10 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 2, []int{1, 1}}, } // 开始测试 @@ -30,10 +28,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }