Skip to content

Commit

Permalink
Merge pull request youngyangyang04#418 from jojoo15/patch-36
Browse files Browse the repository at this point in the history
优化 0738.单调递增的数字 python3
  • Loading branch information
youngyangyang04 committed Jun 21, 2021
2 parents 4cec106 + 31ac198 commit b33e7b3
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions problems/0738.单调递增的数字.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,15 @@ class Solution {


Python:
```python
```python3
class Solution:
def monotoneIncreasingDigits(self, n: int) -> int:
strNum = list(str(n))
flag = len(strNum)
for i in range(len(strNum) - 1, 0, -1):
if int(strNum[i]) < int(strNum[i - 1]):
strNum[i - 1] = str(int(strNum[i - 1]) - 1)
flag = i
for i in range(flag, len(strNum)):
strNum[i] = '9'
return int("".join(strNum))
a = list(str(n))
for i in range(len(a)-1,0,-1):
if int(a[i]) < int(a[i-1]):
a[i-1] = str(int(a[i-1]) - 1)
a[i:] = '9' * (len(a) - i) #python不需要设置flag值,直接按长度给9就好了
return int("".join(a))
```

Go:
Expand Down

0 comments on commit b33e7b3

Please sign in to comment.