Skip to content

Commit 7eccf9f

Browse files
authored
Merge pull request #842 from 0xff-dev/738
Add solution and test-cases for problem 738
2 parents 26b6232 + 62e17cc commit 7eccf9f

File tree

3 files changed

+52
-22
lines changed

3 files changed

+52
-22
lines changed

leetcode/701-800/0738.Monotone-Increasing-Digits/README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# [738.Monotone Increasing Digits][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+
An integer has **monotone increasing digits** if and only if each pair of adjacent digits `x` and `y` satisfy `x <= y`.
5+
6+
Given an integer `n`, return the largest number that is less than or equal to `n` with **monotone increasing digits**.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: n = 10
12+
Output: 9
1313
```
1414

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

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Monotone Increasing Digits
23-
```go
17+
```
18+
Input: n = 1234
19+
Output: 1234
2420
```
2521

22+
**Example 3:**
23+
24+
```
25+
Input: n = 332
26+
Output: 299
27+
```
2628

2729
## 结语
2830

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "fmt"
4+
5+
func Solution(n int) int {
6+
ns := []byte(fmt.Sprintf("%d", n))
7+
idx := 1
8+
for ; idx < len(ns); idx++ {
9+
if ns[idx] < ns[idx-1] {
10+
break
11+
}
12+
}
13+
if idx != len(ns) {
14+
index := idx - 1
15+
top := ns[index]
16+
pre := index - 1
17+
for ; pre >= 0 && ns[pre] == top; pre-- {
18+
ns[pre] = '9'
19+
}
20+
ns[index] = '9'
21+
ns[pre+1] = top - 1
22+
for ; idx < len(ns); idx++ {
23+
ns[idx] = '9'
24+
}
25+
}
26+
27+
ans := 0
28+
for i := 0; i < len(ns); i++ {
29+
ans = ans*10 + int(ns[i]-'0')
30+
}
31+
return ans
532
}

leetcode/701-800/0738.Monotone-Increasing-Digits/Solution_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ 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, 9},
17+
{"TestCase2", 1234, 1234},
18+
{"TestCase3", 332, 299},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,11 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
39+
3940
}

0 commit comments

Comments
 (0)