From 85ccc040edbff116cb043fb5fb5c2fbc2b4c1861 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sun, 5 Oct 2025 23:18:29 +0800 Subject: [PATCH] Add solution and test-cases for problem 3079 --- .../README.md | 27 ++++++++++--------- .../Solution.go | 23 ++++++++++++++-- .../Solution_test.go | 13 +++++---- 3 files changed, 41 insertions(+), 22 deletions(-) diff --git a/leetcode/3001-3100/3079.Find-the-Sum-of-Encrypted-Integers/README.md b/leetcode/3001-3100/3079.Find-the-Sum-of-Encrypted-Integers/README.md index 3b8436adb..90354cef6 100755 --- a/leetcode/3001-3100/3079.Find-the-Sum-of-Encrypted-Integers/README.md +++ b/leetcode/3001-3100/3079.Find-the-Sum-of-Encrypted-Integers/README.md @@ -1,28 +1,29 @@ # [3079.Find the Sum of Encrypted 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 +You are given an integer array `nums` containing **positive** integers. We define a function **encrypt** such that `encrypt(x)` replaces **every** digit in `x` with the **largest** digit in `x`. For example, `encrypt(523) = 555` and `encrypt(213) = 333`. + +Return the **sum** of encrypted elements. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" -``` +Input: nums = [1,2,3] -## 题意 -> ... +Output: 6 -## 题解 +Explanation: The encrypted elements are [1,2,3]. The sum of encrypted elements is 1 + 2 + 3 == 6. +``` + +**Example 2:** -### 思路1 -> ... -Find the Sum of Encrypted Integers -```go ``` +Input: nums = [10,21,31] + +Output: 66 +Explanation: The encrypted elements are [11,22,33]. The sum of encrypted elements is 11 + 22 + 33 == 66. +``` ## 结语 diff --git a/leetcode/3001-3100/3079.Find-the-Sum-of-Encrypted-Integers/Solution.go b/leetcode/3001-3100/3079.Find-the-Sum-of-Encrypted-Integers/Solution.go index d115ccf5e..4095a4d37 100644 --- a/leetcode/3001-3100/3079.Find-the-Sum-of-Encrypted-Integers/Solution.go +++ b/leetcode/3001-3100/3079.Find-the-Sum-of-Encrypted-Integers/Solution.go @@ -1,5 +1,24 @@ package Solution -func Solution(x bool) bool { - return x +func to(n int) int { + x, bits, mod := 0, 0, 0 + for n > 0 { + mod = n % 10 + n /= 10 + x = max(x, mod) + bits++ + } + base := 0 + for ; bits > 0; bits-- { + base = base*10 + x + } + return base +} + +func Solution(nums []int) int { + var ret int + for _, n := range nums { + ret += to(n) + } + return ret } diff --git a/leetcode/3001-3100/3079.Find-the-Sum-of-Encrypted-Integers/Solution_test.go b/leetcode/3001-3100/3079.Find-the-Sum-of-Encrypted-Integers/Solution_test.go index 14ff50eb4..d079673fe 100644 --- a/leetcode/3001-3100/3079.Find-the-Sum-of-Encrypted-Integers/Solution_test.go +++ b/leetcode/3001-3100/3079.Find-the-Sum-of-Encrypted-Integers/Solution_test.go @@ -10,12 +10,11 @@ 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", []int{1, 2, 3}, 6}, + {"TestCase2", []int{10, 21, 31}, 66}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }