Skip to content

Commit b33e7b3

Browse files
Merge pull request #418 from jojoo15/patch-36
优化 0738.单调递增的数字 python3
2 parents 4cec106 + 31ac198 commit b33e7b3

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

problems/0738.单调递增的数字.md

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,15 @@ class Solution {
147147

148148

149149
Python:
150-
```python
150+
```python3
151151
class Solution:
152152
def monotoneIncreasingDigits(self, n: int) -> int:
153-
strNum = list(str(n))
154-
flag = len(strNum)
155-
for i in range(len(strNum) - 1, 0, -1):
156-
if int(strNum[i]) < int(strNum[i - 1]):
157-
strNum[i - 1] = str(int(strNum[i - 1]) - 1)
158-
flag = i
159-
for i in range(flag, len(strNum)):
160-
strNum[i] = '9'
161-
return int("".join(strNum))
153+
a = list(str(n))
154+
for i in range(len(a)-1,0,-1):
155+
if int(a[i]) < int(a[i-1]):
156+
a[i-1] = str(int(a[i-1]) - 1)
157+
a[i:] = '9' * (len(a) - i) #python不需要设置flag值,直接按长度给9就好了
158+
return int("".join(a))
162159
```
163160

164161
Go:

0 commit comments

Comments
 (0)