-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #842 from 0xff-dev/738
Add solution and test-cases for problem 738
- Loading branch information
Showing
3 changed files
with
52 additions
and
22 deletions.
There are no files selected for viewing
28 changes: 15 additions & 13 deletions
28
leetcode/701-800/0738.Monotone-Increasing-Digits/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 29 additions & 2 deletions
31
leetcode/701-800/0738.Monotone-Increasing-Digits/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters