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