Skip to content

Commit 85ccc04

Browse files
committed
Add solution and test-cases for problem 3079
1 parent f03226b commit 85ccc04

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

leetcode/3001-3100/3079.Find-the-Sum-of-Encrypted-Integers/README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
# [3079.Find the Sum of Encrypted 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+
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`.
5+
6+
Return the **sum** of encrypted elements.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
11+
Input: nums = [1,2,3]
1412
15-
## 题意
16-
> ...
13+
Output: 6
1714
18-
## 题解
15+
Explanation: The encrypted elements are [1,2,3]. The sum of encrypted elements is 1 + 2 + 3 == 6.
16+
```
17+
18+
**Example 2:**
1919

20-
### 思路1
21-
> ...
22-
Find the Sum of Encrypted Integers
23-
```go
2420
```
21+
Input: nums = [10,21,31]
22+
23+
Output: 66
2524
25+
Explanation: The encrypted elements are [11,22,33]. The sum of encrypted elements is 11 + 22 + 33 == 66.
26+
```
2627

2728
## 结语
2829

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

3-
func Solution(x bool) bool {
4-
return x
3+
func to(n int) int {
4+
x, bits, mod := 0, 0, 0
5+
for n > 0 {
6+
mod = n % 10
7+
n /= 10
8+
x = max(x, mod)
9+
bits++
10+
}
11+
base := 0
12+
for ; bits > 0; bits-- {
13+
base = base*10 + x
14+
}
15+
return base
16+
}
17+
18+
func Solution(nums []int) int {
19+
var ret int
20+
for _, n := range nums {
21+
ret += to(n)
22+
}
23+
return ret
524
}

leetcode/3001-3100/3079.Find-the-Sum-of-Encrypted-Integers/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", []int{1, 2, 3}, 6},
17+
{"TestCase2", []int{10, 21, 31}, 66},
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)