优化 0738.单调递增的数字 python3

优化 0738.单调递增的数字 python3
This commit is contained in:
jojoo15
2021-06-17 18:11:09 +02:00
committed by GitHub
parent 7bbc8ba877
commit 31ac198cd1

View File

@ -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