Skip to content

Commit

Permalink
Merge pull request #842 from 0xff-dev/738
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 738
  • Loading branch information
6boris committed Jun 6, 2024
2 parents 26b6232 + 62e17cc commit 7eccf9f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
28 changes: 15 additions & 13 deletions leetcode/701-800/0738.Monotone-Increasing-Digits/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
# [738.Monotone Increasing Digits][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
An integer has **monotone increasing digits** if and only if each pair of adjacent digits `x` and `y` satisfy `x <= y`.

Given an integer `n`, return the largest number that is less than or equal to `n` with **monotone increasing digits**.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: n = 10
Output: 9
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Monotone Increasing Digits
```go
```
Input: n = 1234
Output: 1234
```

**Example 3:**

```
Input: n = 332
Output: 299
```

## 结语

Expand Down
31 changes: 29 additions & 2 deletions leetcode/701-800/0738.Monotone-Increasing-Digits/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
package Solution

func Solution(x bool) bool {
return x
import "fmt"

func Solution(n int) int {
ns := []byte(fmt.Sprintf("%d", n))
idx := 1
for ; idx < len(ns); idx++ {
if ns[idx] < ns[idx-1] {
break
}
}
if idx != len(ns) {
index := idx - 1
top := ns[index]
pre := index - 1
for ; pre >= 0 && ns[pre] == top; pre-- {
ns[pre] = '9'
}
ns[index] = '9'
ns[pre+1] = top - 1
for ; idx < len(ns); idx++ {
ns[idx] = '9'
}
}

ans := 0
for i := 0; i < len(ns); i++ {
ans = ans*10 + int(ns[i]-'0')
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ 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", 10, 9},
{"TestCase2", 1234, 1234},
{"TestCase3", 332, 299},
}

// 开始测试
Expand All @@ -30,10 +30,11 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {

}

0 comments on commit 7eccf9f

Please sign in to comment.