From 62e17ccae24a0e751c52d83f4fdd2c2b8a390c10 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Thu, 11 Apr 2024 23:56:08 +0800 Subject: [PATCH] Add solution and test-cases for problem 738 --- .../0738.Monotone-Increasing-Digits/README.md | 28 +++++++++-------- .../Solution.go | 31 +++++++++++++++++-- .../Solution_test.go | 15 ++++----- 3 files changed, 52 insertions(+), 22 deletions(-) diff --git a/leetcode/701-800/0738.Monotone-Increasing-Digits/README.md b/leetcode/701-800/0738.Monotone-Increasing-Digits/README.md index af6342d5a..025d07806 100644 --- a/leetcode/701-800/0738.Monotone-Increasing-Digits/README.md +++ b/leetcode/701-800/0738.Monotone-Increasing-Digits/README.md @@ -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 +``` ## 结语 diff --git a/leetcode/701-800/0738.Monotone-Increasing-Digits/Solution.go b/leetcode/701-800/0738.Monotone-Increasing-Digits/Solution.go index d115ccf5e..4ba70b630 100644 --- a/leetcode/701-800/0738.Monotone-Increasing-Digits/Solution.go +++ b/leetcode/701-800/0738.Monotone-Increasing-Digits/Solution.go @@ -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 } diff --git a/leetcode/701-800/0738.Monotone-Increasing-Digits/Solution_test.go b/leetcode/701-800/0738.Monotone-Increasing-Digits/Solution_test.go index 14ff50eb4..6369cc245 100644 --- a/leetcode/701-800/0738.Monotone-Increasing-Digits/Solution_test.go +++ b/leetcode/701-800/0738.Monotone-Increasing-Digits/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +30,11 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { + }